diff options
| author | Caio Ariede <caio.ariede@gmail.com> | 2019-11-18 10:53:30 -0300 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-07-13 23:02:51 +0200 |
| commit | 96b04f53c56d912b491cd06a3ec776e1195c6308 (patch) | |
| tree | 0313149f90aa3a11f85eaf0a239b40def298beb0 | |
| parent | b44e2d62c0993589726914b4edf8f6063abac257 (diff) | |
[3.0.x] Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.
Backport of d38c34119e91a533c797098f150abe99b5ee2fd8 from master
| -rw-r--r-- | docs/ref/contrib/admin/index.txt | 12 | ||||
| -rw-r--r-- | docs/ref/forms/fields.txt | 2 |
2 files changed, 14 insertions, 0 deletions
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 01184a4374..0b0085b8c0 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -1782,6 +1782,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 diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 1d08e2d069..5f53becce8 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -1098,6 +1098,8 @@ Slightly complex built-in ``Field`` classes If no ``input_time_formats`` argument is provided, the default input formats for :class:`TimeField` are used. +.. _fields-which-handle-relationships: + Fields which handle relationships ================================= |
