mirror of
https://github.com/django/django.git
synced 2025-07-24 05:36:15 +00:00
Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.
This commit is contained in:
parent
e906ff6fca
commit
d38c34119e
2 changed files with 14 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue