Django doesn't support getting foreign key values from list_display or list_filter (e.g foo__bar). I know you can create a module method as a workaround for list_display, but how would I go about to do the same for list_filter? Thanks.
相关问题
- Django __str__ returned non-string (type NoneType)
- how to split a list into a given number of sub-lis
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
相关文章
- List可以存储接口类型的数据吗?
-
C# List
.FindAll 时 空指针异常 - What is the complexity of bisect algorithm?
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Given a list and a bitmask, how do I return the va
- Django/Heroku: FATAL: too many connections for rol
I ran into the same problem and really needed a solution. I have a workaround that lets you create a filter on a FK related model property. You can even traverse more than one FK relationship. It creates a new FilterSpec subclass that subclasses the default RelatedFilterSpec used to give you a filter on a ForeignKey field.
See http://djangosnippets.org/snippets/2260/
Well, the docs say that you can may use
ForeignKey
field types inlist_filter
:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
An example:
If you want to filter by a field from the related model, there's a patch on the way to make this work (will probably be merged into 1.2 as it seems):
http://code.djangoproject.com/ticket/3400
solution from this page worked for me http://www.hoboes.com/Mimsy/hacks/fixing-django-124s-suspiciousoperation-filtering/
define
then allow the lookup for certain foreign key field
Django supports list_filter with foreign key fields
From documentation: Field names in list_filter can also span relations using the __ lookup
If you construct the URL for the changelist manually then Django has no problems following relationships. For example:
or
Both work fine (although the latter doesn't add 'distinct()' so might have duplicates but that won't usually be an issue for filters)
So you just need to add something to the page that creates the correct links. You can do this either with by overriding the changelist template or by writing a custom filterspec. There are several examples I found by Googling - particularly on Django Snippets
You can easily create custom filters since Django 1.4 by overriding
django.contrib.admin.SimpleListFilter
class.More information :