Login

Snippets by darklow

Snippet List

Custom change_list filter based on SimpleListFilter shows only referenced (related, used) values

Since Django 1.4 you can create your own filters for change list view. If you want to show just used/related items in filter chooser you can use this snippet. Original idea from [here](http://jmduke.net/post/39953950546/custom-admin-filters-in-django). Big thanks to author. Improved class names for better clarity and use of model_admin.model instead of hardcoded model name. In example you can see two models - City and Country. City has ForeignKey to Country. If you use regular list_filter = ('country',) you will have all the countries in the chooser. This snippet however filters only related countries - the ones that have at least one relation to city.

  • ForeignKey
  • Filter
  • SimpleListFilter
Read More

darklow has posted 1 snippet.