how to edit model data using django forms

后端 未结 1 1288
深忆病人
深忆病人 2020-12-04 20:19

i\'m new to django so i\'m sorry for my newbie question
i have a model and i need to let user edit data inside it using django forms or any other way.

相关标签:
1条回答
  • 2020-12-04 20:38

    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/

    0 讨论(0)
提交回复
热议问题