From abce5abe6686551e68f7e8a663f8918c56d307b1 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 8 Jul 2013 08:29:28 -0400 Subject: [PATCH] [1.5.x] Fixed #12346 -- Added a note on how to validate InlineFormSets. Thanks johnsmith for the suggestion. Backport of 181f63c22d from master --- docs/ref/contrib/admin/index.txt | 11 ++++++----- docs/ref/forms/models.txt | 4 ++-- docs/topics/forms/modelforms.txt | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 07a5965c0cf4f..ea2d247c0bd65 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -1585,9 +1585,9 @@ The ``InlineModelAdmin`` class adds: .. attribute:: InlineModelAdmin.formset - This defaults to ``BaseInlineFormSet``. Using your own formset can give you - many possibilities of customization. Inlines are built around - :ref:`model formsets `. + This defaults to :class:`~django.forms.models.BaseInlineFormSet`. Using + your own formset can give you many possibilities of customization. Inlines + are built around :ref:`model formsets `. .. attribute:: InlineModelAdmin.form @@ -1653,8 +1653,9 @@ The ``InlineModelAdmin`` class adds: .. method:: InlineModelAdmin.get_formset(self, request, obj=None, **kwargs) - Returns a ``BaseInlineFormSet`` class for use in admin add/change views. - See the example for :class:`ModelAdmin.get_formsets`. + Returns a :class:`~django.forms.models.BaseInlineFormSet` class for use in + admin add/change views. See the example for + :class:`ModelAdmin.get_formsets`. Working with a model with two or more foreign keys to the same parent model --------------------------------------------------------------------------- diff --git a/docs/ref/forms/models.txt b/docs/ref/forms/models.txt index c388f402e6b29..3d835210911c8 100644 --- a/docs/ref/forms/models.txt +++ b/docs/ref/forms/models.txt @@ -42,8 +42,8 @@ Model Form Functions .. function:: inlineformset_factory(parent_model, model, form=ModelForm, formset=BaseInlineFormSet, fk_name=None, fields=None, exclude=None, extra=3, can_order=False, can_delete=True, max_num=None, formfield_callback=None) Returns an ``InlineFormSet`` using :func:`modelformset_factory` with - defaults of ``formset=BaseInlineFormSet``, ``can_delete=True``, and - ``extra=3``. + defaults of ``formset=``:class:`~django.forms.models.BaseInlineFormSet`, + ``can_delete=True``, and ``extra=3``. If your model has more than one :class:`~django.db.models.ForeignKey` to the ``parent_model``, you must specify a ``fk_name``. diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 2c8c30ebe8c38..be17a518c3966 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -754,14 +754,16 @@ than that of a "normal" formset. The only difference is that we call ``formset.save()`` to save the data into the database. (This was described above, in :ref:`saving-objects-in-the-formset`.) -Overiding ``clean()`` on a ``model_formset`` +.. _model-formsets-overriding-clean: + +Overriding ``clean()`` on a ``ModelFormSet`` -------------------------------------------- Just like with ``ModelForms``, by default the ``clean()`` method of a -``model_formset`` will validate that none of the items in the formset violate +``ModelFormSet`` will validate that none of the items in the formset violate the unique constraints on your model (either ``unique``, ``unique_together`` or ``unique_for_date|month|year``). If you want to override the ``clean()`` method -on a ``model_formset`` and maintain this validation, you must call the parent +on a ``ModelFormSet`` and maintain this validation, you must call the parent class's ``clean`` method:: class MyModelFormSet(BaseModelFormSet): @@ -859,6 +861,8 @@ primary key that isn't called ``id``, make sure it gets rendered.) Inline formsets =============== +.. class:: models.BaseInlineFormSet + Inline formsets is a small abstraction layer on top of model formsets. These simplify the case of working with related objects via a foreign key. Suppose you have these two models:: @@ -888,6 +892,13 @@ a particular author, you could do this:: :ref:`Manually rendered can_delete and can_order `. +Overriding ``clean()`` on an ``InlineFormSet`` +---------------------------------------------- + +See :ref:`model-formsets-overriding-clean`, but subclass +:class:`~models.BaseInlineFormSet` rather than +:class:`~models.BaseModelFormSet`. + More than one foreign key to the same model -------------------------------------------