开发者

Django admin list filter

开发者 https://www.devze.com 2022-12-21 18:58 出处:网络
I want to add a custom model method to the admin filter, however it fails. Example Foo: class Foo(models.Model):

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号