mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.
This commit is contained in:
committed by
Mariusz Felisiak
parent
e906ff6fca
commit
d38c34119e
@@ -1812,6 +1812,18 @@ templates used by the :class:`ModelAdmin` views:
|
||||
This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key
|
||||
field to only display the cars owned by the ``User`` instance.
|
||||
|
||||
For more complex filters, you can use ``ModelForm.__init__()`` method to
|
||||
filter based on an ``instance`` of your model (see
|
||||
:ref:`fields-which-handle-relationships`). For example::
|
||||
|
||||
class CountryAdminForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['capital'].queryset = self.instance.cities.all()
|
||||
|
||||
class CountryAdmin(admin.ModelAdmin):
|
||||
form = CountryAdminForm
|
||||
|
||||
.. method:: ModelAdmin.formfield_for_manytomany(db_field, request, **kwargs)
|
||||
|
||||
Like the ``formfield_for_foreignkey`` method, the
|
||||
|
||||
Reference in New Issue
Block a user