mirror of https://github.com/django/django.git
[3.1.x] Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.
Backport of d38c34119e
from master
This commit is contained in:
parent
7045fa7f7a
commit
7fe7d83295
|
@ -1802,6 +1802,18 @@ templates used by the :class:`ModelAdmin` views:
|
||||||
This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key
|
This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key
|
||||||
field to only display the cars owned by the ``User`` instance.
|
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)
|
.. method:: ModelAdmin.formfield_for_manytomany(db_field, request, **kwargs)
|
||||||
|
|
||||||
Like the ``formfield_for_foreignkey`` method, the
|
Like the ``formfield_for_foreignkey`` method, the
|
||||||
|
|
|
@ -1174,6 +1174,8 @@ Slightly complex built-in ``Field`` classes
|
||||||
If no ``input_time_formats`` argument is provided, the default input formats
|
If no ``input_time_formats`` argument is provided, the default input formats
|
||||||
for :class:`TimeField` are used.
|
for :class:`TimeField` are used.
|
||||||
|
|
||||||
|
.. _fields-which-handle-relationships:
|
||||||
|
|
||||||
Fields which handle relationships
|
Fields which handle relationships
|
||||||
=================================
|
=================================
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue