How do I create a filter in Django Admin to only display records where an integer value lies between two values? For example, if I have a model Person, which has an age attribute, and I only want to display Person records where age is between 45 and 65.
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
- UnicodeEncodeError when saving ImageField containi
I you simply want a filtered version of the list view, that you access via a link (say in the list view), for example to view only the related items of a model, you do something like this:
This will take you to a list view for 'Reciepts', but only those linked to the selected Invoice.
If you want a filter that displays in the sidebar, you could try this snippet or this
Based on another answer for a related question, I learnt that there is an officially documented way to do that since version 1.4. It even includes an example of filtering by date.
Still, the snippet in the sorin answer is also interesting, because it just adds django-style parameters to the URL, which is a different solution than the official documentation example.
What you are looking is http://djangosnippets.org/snippets/587/ - the snippet is kinda old but works just fine after an additional minor change.
I uploaded the patched version at https://gist.github.com/1009903
You can Filter the field some what like this by using
queryset()
Function ... I had usedSimpleListFilter
And create dict in
lookups()
and return it According to the age