Get Model instance from Form without saving
Let say I have a dja开发者_如何学编程ngo ModelForm which I want to edit before saving.
For example,
Instead of this
model_instance = form.save()
I would like to do something like this
model_instance = form.get_model() model_instance.edit() #say add a new field which is not available on form model_instance.save()
model_instance = form.save(commit=False)
will return you a object of the model without saving to the DB
you can then add value of some field which is not available on form
model_instance.some_field = value
model_instance.save()
Also:
model_instance = form.instance
# edit
model_instance.save()
精彩评论