[Django]-How to edit model data using django forms

67👍

Assuming you are using a ModelForm, use the instance keyword argument, and pass the model you are updating.

So, if you have MyModel and MyModelForm (the latter of which must extend django.forms.ModelForm), then your code snippet might look like:

my_record = MyModel.objects.get(id=XXX)
form = MyModelForm(instance=my_record)

And then, when the user sends back data by POST:

form = MyModelForm(request.POST, instance=my_record)

Incidentally, the documentation for ModelForm is here: http://docs.djangoproject.com/en/1.8/topics/forms/modelforms/

Leave a comment