I think that what that
http://docs.djangoproject.com/en/dev/releases/1.2/#modelform-is-valid...
is saying is that if you don't want your instance to get modified by
is_valid(), then when you initially create your ModelForm, you should
pass in a copy of your instance.  Something like this:

  instanceCopy = deepcopy(instance)
  form = MyFormClass(instance=instanceCopy, data=data)

That way when you call form.is_valid(), it will write to instanceCopy,
not instance.  However, I don't think just doing a deepcopy() will
work if your instance has m2m fields.  You end up copying a pointer to
the m2m field, and both instanceCopy and instance will point to the
same m2m field, and if that m2m field then gets modified by the save
associated with is_valid(), your instanceCopy m2m field will look
modified as well.  I would be curious to know what the is the
appropriate tact to take on this.

In my case, I want to compare the saved instance to the pre-clean
instance.  Previously I did this in my save() method, but now I guess
I have to make a copy of the instance prior to creating my form.  This
is shame because it means I have to copy the instance even in the case
where I am not ultimately going to save to the database.  For example,
in the case where the user submitted form has an error.  I now have no
entry point that allows me to identify there is an error, and then
make the decision about whether to make the instance copy.

In general, I find it very problematic that I now have no ability to
make a decision or take any action after form validation, but prior to
instance save.  It seems like all sorts of workarounds are needed in
one's code if you have requirements that previously (in 1.1) could be
fulfilled by taking some action based on form validation, but prior to
instance save.

However, as Russell has noted, I am sadly very late in making this
observation.

Margie

On Jul 13, 8:28 am, Alaa Abd El Fattah <a...@translate.org.za> wrote:
> On Tue, 13 Jul 2010 02:10:03 -0700 (PDT)
>
> Margie <margierogin...@gmail.com> wrote:
> > It's hard for me to believe that no other developers have encountered
> > this same problem (the problem of having a ModelForm saved at a point
> > in the code where you have no control).
>
> I did face the same problem in a more straightforward application, I've
> a model with multiple submits, only one of them should actually modify
> the model instance. now the moment is_clean() is executed the instance
> gets modified. wasn't fun to track the related bugs.
>
> I read about this backward incompatible change 
> inhttp://docs.djangoproject.com/en/dev/releases/1.2/#modelform-is-valid...
> but did not understand what it meant except in hindsight. I'm
> actually still not sure what the advice to pass a copy to the ModelForm
> constructor means (I suppose it could mean override __init__ and
> use copy.copy).
>
> cheers,
> Alaa

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

Reply via email to