1.2 ModelForm.is_valid() backward incompatible change

38 views
Skip to first unread message

ydjango

unread,
Jun 9, 2012, 4:54:48 PM6/9/12
to Django users
From 1.2 release notes (backward incompatible change):
"Much of the validation work for ModelForms has been moved down to the
model level. As a result, the first time you call
ModelForm.is_valid(), access ModelForm.errors or otherwise trigger
form validation, your model will be cleaned in-place. This conversion
used to happen when the model was saved. If you need an unmodified
instance of your model, you should pass a copy to the ModelForm
constructor.

I am migrating from 1.1 to 1.3. Some of my code rely on model not
being cleaned in place. As some fields become read only and should
not be changed. This 1.2 functionality makes those field empty. Also I
save the model and not the form in these cases.

So as per documentation, to get around it, I need to pass a copy of
model to form. What is the best way to get a copy of model:
1) use deepcopy?
2) get the object twice from db? (slow as more db access)

Any downsides to using deepcopy? Anything I need to be careful about?

Anyone else also had this issue and any suggestions.

Kurtis Mullins

unread,
Jun 12, 2012, 10:29:13 AM6/12/12
to django...@googlegroups.com
I'm not sure if I understand the problem correctly, but I think this might be what you're looking to do.

In your ModelForm, simply exclude the fields you don't want or explicitly include only the fields you do want to use. Those are both under the ModelForm's Meta class.

e.g.

# Include only these fields
class Meta:
    fields = ('fieldA', 'fieldB', 'fieldD')

# Or Exclude a field
class Meta:
    exclude = ('fieldC',)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages