mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #19733 - deprecated ModelForms without 'fields' or 'exclude', and added '__all__' shortcut
This also updates all dependent functionality, including modelform_factory and modelformset_factory, and the generic views `ModelFormMixin`, `CreateView` and `UpdateView` which gain a new `fields` attribute.
This commit is contained in:
parent
1e37cb37ce
commit
f026a519ae
34 changed files with 578 additions and 201 deletions
|
@ -335,6 +335,22 @@ subclass::
|
|||
|
||||
For an example see the section `Adding custom validation to the admin`_.
|
||||
|
||||
.. admonition:: Note
|
||||
|
||||
.. versionchanged:: 1.6
|
||||
|
||||
If you define the ``Meta.model`` attribute on a
|
||||
:class:`~django.forms.ModelForm`, you must also define the
|
||||
``Meta.fields`` attribute (or the ``Meta.exclude`` attribute). However,
|
||||
since the admin has its own way of defining fields, the ``Meta.fields``
|
||||
attribute will be ignored.
|
||||
|
||||
If the ``ModelForm`` is only going to be used for the admin, the easiest
|
||||
solution is to omit the ``Meta.model`` attribute, since ``ModelAdmin``
|
||||
will provide the correct model to use. Alternatively, you can set
|
||||
``fields = []`` in the ``Meta`` class to satisfy the validation on the
|
||||
``ModelForm``.
|
||||
|
||||
.. admonition:: Note
|
||||
|
||||
If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude``
|
||||
|
@ -1283,13 +1299,24 @@ templates used by the :class:`ModelAdmin` views:
|
|||
on the changelist page. To use a custom form, for example::
|
||||
|
||||
class MyForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = MyModel
|
||||
pass
|
||||
|
||||
class MyModelAdmin(admin.ModelAdmin):
|
||||
def get_changelist_form(self, request, **kwargs):
|
||||
return MyForm
|
||||
|
||||
.. admonition:: Note
|
||||
|
||||
.. versionchanged:: 1.6
|
||||
|
||||
If you define the ``Meta.model`` attribute on a
|
||||
:class:`~django.forms.ModelForm`, you must also define the
|
||||
``Meta.fields`` attribute (or the ``Meta.exclude`` attribute). However,
|
||||
``ModelAdmin`` ignores this value, overriding it with the
|
||||
:attr:`ModelAdmin.list_editable` attribute. The easiest solution is to
|
||||
omit the ``Meta.model`` attribute, since ``ModelAdmin`` will provide the
|
||||
correct model to use.
|
||||
|
||||
.. method:: ModelAdmin.get_changelist_formset(self, request, **kwargs)
|
||||
|
||||
Returns a :ref:`ModelFormSet <model-formsets>` class for use on the
|
||||
|
@ -1490,9 +1517,6 @@ needed. Now within your form you can add your own custom validation for
|
|||
any field::
|
||||
|
||||
class MyArticleAdminForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Article
|
||||
|
||||
def clean_name(self):
|
||||
# do something that validates your data
|
||||
return self.cleaned_data["name"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue