Improved docs for ModelFormSet.clean().

This commit is contained in:
Loic Bistuer 2013-11-22 19:55:02 +07:00 committed by Tim Graham
parent c456ea4ec8
commit 033b26173b
1 changed files with 18 additions and 0 deletions

View File

@ -943,6 +943,24 @@ class's ``clean`` method::
# your custom formset validation
...
Also note that by the time you reach this step, individual model instances
have already been created for each ``Form``. Modifying a value in
``form.cleaned_data`` is not sufficient to affect the saved value. If you wish
to modify a value in ``ModelFormSet.clean()`` you must modify
``form.instance``::
from django.forms.models import BaseModelFormSet
class MyModelFormSet(BaseModelFormSet):
def clean(self):
super(MyModelFormSet, self).clean()
for form in self.forms:
name = form.cleaned_data['name'].upper()
form.cleaned_data['name'] = name
# update the instance value.
form.instance.name = name
Using a custom queryset
-----------------------