mirror of
https://github.com/django/django.git
synced 2025-07-17 16:19:12 +00:00
[3.0.x] Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.
Backport of d38c34119e91a533c797098f150abe99b5ee2fd8 from master
This commit is contained in:
parent
b44e2d62c0
commit
96b04f53c5
@ -1782,6 +1782,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
|
||||||
|
@ -1098,6 +1098,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…
x
Reference in New Issue
Block a user