Login

Tag "ForeignKey"

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

Limit ForeignKey filter values to those that have a relationship with current model

This is an updated snippet based on http://djangosnippets.org/snippets/2260/ The updated snippet can limit the filtering options for a foreign key field to only those entries that are related to the current model. I.e. if you have an Author model with a FK to Institution model, you can configure Author's changelist to include a filter on Institution, but only allow you to select institutions that have authors. Institutions that do not have authors won't show up on the list. To enable this, in your model's ModelAdmin class, set <fieldname>_fk_filter_related_only=True <fieldname>_fk_filter_name_field=<display this field of the related model in filter list> For example, in your AuthorAdmin class, you can do institution_fk_filter_related_only=True institution_fk_filter_name_field='name' Note that for the effect described above to work, you just need the last few lines of the big else clause in __init__, so if you don't care about filtering by FK property, you can just grab those few lines and create a simpler FilterSpec.

  • ForeignKey
  • Django-Admin
  • FilterSpec
Read More

RelatedNullFilterSpec: django-admin custom filter all/null/not null/choices

A simple django-admin filter to replace standar RelatedFilterSpec with one with the "All"/"Null"/"Not null" options. It applies to all relational fields (ForeignKey, ManyToManyField). You can put the code in the `__init__.py` or wherever you want. The `_register_front` idea is copied on [this snippet](http://djangosnippets.org/snippets/1963/)

  • filter
  • admin
  • null
  • filterspec
  • ManyToManyField
  • ForeignKey
  • not-null
Read More

Link raw_id_fields (both ForeignKeys and ManyToManyFields) to their change pages

**UPDATE: Now works in Django 1.4** Based on luc_j:s snippet (http://djangosnippets.org/snippets/2108/) to show values in ManyToManyFields in the admin. This snippets does that, but also links each value to its corresponding admin change page. To use, just set the raw_id_fields to the value you want, and let your form inherit from ImproveRawIdFieldsForm.

  • admin
  • widget
  • django-admin
  • raw_id_fields
  • ManyToManyField
  • ForeignKey
Read More

5 snippets posted so far.