tags:

views:

77

answers:

1

I wish to update a model instance from a form I have.

The form is a ModelForm, so it has the same attributes as the model instance, how do I transfer the attributes from the form instance to the model instance instead of doing this:

modelinstance.name = form.name . . . .

A for loop perhaps? :)

Thanks!

+6  A: 

Call the save() method of the form. Specifically instantiate the form with keyword argument instance like this:

>>> a = Article.objects.get(pk=1)
>>> f = ArticleForm(instance=a)
>>> f.save()

Taken from here: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

stefanw