Django admin, a logic-based list filter

On the admin model object, I have a function called that returns True or False. I want to be able to use this function to filter what is displayed in a list (i.e. List_filter). However, the code below will not work, because you can only use list_filter for fields:

 ... class FooAdmin(admin.ModelAdmin): ... list_filter['bar'] def bar(self, obj): x = ... #something boolean return x ... 


Is it possible to use True / False to filter the list in admin? Or should you denormalize your data if you want this functionality?

I noticed that in development docs this is now possible: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

However, in 1.3 documents (the version of Django that I use) he does not mention this: https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter So, I am assuming that I cannot use the new functionality with my project: - (

+4
source share
1 answer

If you can somehow express the function of your bar function in terms of searching with double underlining ORM, then you can create FilterSpec in Django 1.3

See django.contrib.admin.filterspecs

These classes handle the creation of a list of filter options and prepare the request value for the URL, etc. As far as I can tell, they work by providing the field_path attribute, which is used by other parts of the admin code to filter the set of change requests.

An example of a custom FilterSpec filter:
http://djangosnippets.org/snippets/2644/

+2
source

Source: https://habr.com/ru/post/1384970/


All Articles