I found the ModelForm in Django is very easy to use and it saves great time of development.
However, I was stuck when I realize the is_valid actually saves the ModelForm! I would like to know if this is expected behavior, or am I doing something wrong?
What happens to me is
form=SOME_MODEL_FORM(...., instance=cafe)
print cafe.name # "CAFE OLD NAME"
if request.method="POST":
if form.is_valid():
### HERE the cafe instance has been changed
print cafe.name # "CAFE NEW NAME"
I use post_save to debug, the is_valid did save the model!
My current workaround is to save the model in another object before calling is_valid, then save back to override the chang. It is really hacking and I would like to have an more elegant way to achieve the same goal (not save the model after is_valid call).
Thanks!
No
form.is_valid()does not save the new data in DB. However, it updates the instance object with new attributes so that it can use them when you callform.save().Here is how you can verify this: