Django admin list filter

14,788

Solution 1

See this SO thread. It's not as easy as it feels like it should be.

Solution 2

You cannot use a model method for this purpose. list_filter is used to filter a django queryset, which cannot meaningfully utilize bare functions.

Share:
14,788
Hellnar
Author by

Hellnar

Updated on June 18, 2022

Comments

  • Hellnar
    Hellnar almost 2 years

    I want to add a custom model method to the admin filter, however it fails.

    Example Foo:

    class Foo(models.Model):
         number = models.IntegerField()
         def big_enough(self):
            return self.number > 99
    

    now at the admin panel:

    class FooAdmin(admin.ModelAdmin):
         list_filter = ('number', 'big_enough')
    

    Fails, I get the error

    ImproperlyConfigured at /admin/test/foo/ 'FooAdmin.list_filter[0]' refers to field 'big_enough' that is missing from model 'Foo'.