views:

241

answers:

2

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'.

+1  A: 

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

Tom
A: 

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

vezult