Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin list filter

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

like image 522
Hellnar Avatar asked Mar 04 '10 13:03

Hellnar


People also ask

What is list filter in Django admin?

Django allows the user of the admin site to filter the instances of a Model by adding the list_filter attribute to your ModelAdmin objects. You can find more information about the Django's filtering utilities in the official documentation, in the Django Admin Site section.

What is Admin filter?

Admin Taxonomy Filter helps you to filter posts or custom post types in the admin area (the post list table) by custom taxonomies. It's similar to filter posts by categories, which is supported by default.

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


2 Answers

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

like image 147
Tom Avatar answered Nov 13 '22 06:11

Tom


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

like image 44
vezult Avatar answered Nov 13 '22 06:11

vezult