Option to limit list_filter to related_objects of instances of the list
|Reported by:||Owned by:||nobody|
|Severity:||Normal||Keywords:||admin, list_filter, limit_choices_to|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I know there are many snippets and blog entries to do that but it is a common use case to limit the FK/related model choices to the very objects displayed in the list and maybe the core team would consider to implement it directly in the Django admin.
The +10 lines patch I provide allow you to limit the choices for a FK/M2M to the current object list by adding the attribute
RELATED_FIELD_filter_related_only=True to your ModelAdmin.
By example :
class Order(models.Model): ... support = models.ForeignKey(Support) parutions = models.ManyToManyField(Parution, through='OrderParution', related_name='order') class OrderAdmin(admin.ModelAdmin): ... list_filter = ('support', 'agency', 'parutions') parutions_filter_related_only=True support_filter_related_only=True
Change History (18)
comment:2 Changed 5 years ago by
|Patch needs improvement:||set|
|Triage Stage:||Unreviewed → Accepted|
Changed 5 years ago by