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 20:32:57 +0200 |
| commit | d38c34119e91a533c797098f150abe99b5ee2fd8 (patch) | |
| tree | 8362d05bd5dcfe6de7b017fa44f5e1243d7ac8fc /docs | |
| parent | e906ff6fca291fc0bfa0d52f05817ee9dae0335d (diff) | |
Fixed #21528 -- Added note about filtering form field's queryset based on instance to admin docs.
Diffstat (limited to 'docs')
| -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 ddd2cfbb67..d532d7b337 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -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 diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index d7f3ca17a2..bd80f939e5 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -1174,6 +1174,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 ================================= |
