Question

For example, if I use filter on several foreign key like this:

list_filter = ['status', 'species__name', 'factor__name', 'factor__type', ]

There will be two filter whose name is displayed as "Name" on the admin site.

enter image description here

Does anyone have ideas about how to solve this? (by give them different names.)

I think django.contrib.admin.SimpleListFilter may be a solution, but considering that I have 10+ fields like xxx__name. It might be a little clumsy..

Was it helpful?

Solution

As per my debugging i found that /python2.7/dist-packages/django/contrib/admin/templates/admin/filter.html is the template which is responsible to render filter related content .
in this template you can found

{% blocktrans with filter_title=title %} By {{ filter_title }} {% endblocktrans %}

Here each and every filter's title is coming from filter_title
so to get the value of filter_title you can go to
/python2.7/dist-packages/django/contrib/admin/filters.py
Here in FieldListFilter class you can see it is directly putting verbose_name into filter.title.
Here you have

list_filter = ['status', 'species__name', 'factor__name', 'factor__type', ]

you can solve your problem by simply providing verbose name to fields which belong to other table and connected into your model as Foreignkey .

#Home  here home is first model
name = models.CharField(verbose_name="Home status", --)
# Out  here Out is second model 
name = models.CharField(verbose_name="out status", ---)
#Main this is main model
home = models.ForeignKey(Home)
out = models.ForeignKey(Out)

so now if you will use filter it will display filter title as Home status and out status

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top