Ticket #11128: 11128-model_fset_doc_fixes-r10793.2.diff

File 11128-model_fset_doc_fixes-r10793.2.diff, 4.0 KB (added by Ramiro Morales, 16 years ago)

Added mentions of unique_for_* constraints in clean methods overridding sections as suggested by Alex

  • docs/topics/forms/modelforms.txt

    diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
    a b  
    400400
    401401You can override the ``clean()`` method on a model form to provide additional
    402402validation in the same way you can on a normal form. However, by default the
    403 ``clean()`` method validates the uniqueness of fields that are marked as unique
    404 or unique_together on the model. Therefore, if you would like to override
    405 the ``clean()`` method and maintain the default validation, you must call the
    406 parent class's ``clean()`` method.
     403``clean()`` method validates the uniqueness of fields that are marked as
     404``unique``, ``unique_together`` or ``unique_for_date|month|year`` on the model.
     405Therefore, if you would like to override the ``clean()`` method and maintain the
     406default validation, you must call the parent class's ``clean()`` method.
    407407
    408408Form inheritance
    409409----------------
     
    515515
    516516.. _saving-objects-in-the-formset:
    517517
    518 Overriding clean() method
    519 -------------------------
    520 
    521 You can override the ``clean()`` method to provide custom validation to
    522 the whole formset at once. By default, the ``clean()`` method will validate
    523 that none of the data in the formsets violate the unique constraints on your
    524 model (both field ``unique`` and model ``unique_together``). To maintain this
    525 default behavior be sure you call the parent's ``clean()`` method::
    526 
    527     class MyModelFormSet(BaseModelFormSet):
    528         def clean(self):
    529             super(MyModelFormSet, self).clean()
    530             # example custom validation across forms in the formset:
    531             for form in self.forms:
    532                 # your custom formset validation
    533 
    534518Saving objects in the formset
    535519-----------------------------
    536520
     
    615599``formset.save()`` to save the data into the database. (This was described
    616600above, in :ref:`saving-objects-in-the-formset`.)
    617601
    618 
    619602Overiding ``clean()`` on a ``model_formset``
    620603--------------------------------------------
    621604
    622605Just like with ``ModelForms``, by default the ``clean()`` method of a
    623 ``model_formset`` will validate that none of the items in the formset validate
    624 the unique constraints on your model(either unique or unique_together).  If you
    625 want to overide the ``clean()`` method on a ``model_formset`` and maintain this
    626 validation, you must call the parent classes ``clean`` method.
     606``model_formset`` will validate that none of the items in the formset violate
     607the unique constraints on your model (either ``unique``, ``unique_together`` or
     608``unique_for_date|month|year``).  If you want to overide the ``clean()`` method
     609on a ``model_formset`` and maintain this validation, you must call the parent
     610classes ``clean`` method::
    627611
     612    class MyModelFormSet(BaseModelFormSet):
     613        def clean(self):
     614            super(MyModelFormSet, self).clean()
     615            # example custom validation across forms in the formset:
     616            for form in self.forms:
     617                # your custom formset validation
    628618
    629619Using a custom queryset
    630 ~~~~~~~~~~~~~~~~~~~~~~~
     620-----------------------
    631621
    632622As stated earlier, you can override the default queryset used by the model
    633623formset::
     
    650640cases in this example.
    651641
    652642Using the formset in the template
    653 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     643---------------------------------
     644
     645.. highlight:: html+django
    654646
    655647There are three ways to render a formset in a Django template.
    656648
     
    705697assumes a primary key named ``id``. If you've explicitly defined your own
    706698primary key that isn't called ``id``, make sure it gets rendered.)
    707699
     700.. highlight:: python
     701
    708702Inline formsets
    709703===============
    710704
     
    745739
    746740To resolve this, you can use ``fk_name`` to ``inlineformset_factory``::
    747741
    748     >>> FrienshipFormSet = inlineformset_factory(Friend, Friendship, fk_name="from_friend")
     742    >>> FriendshipFormSet = inlineformset_factory(Friend, Friendship, fk_name="from_friend")
    749743
    750744Using an inline formset in a view
    751745---------------------------------
Back to Top