如何使用django表单编辑模型数据 [英] how to edit model data using django forms

查看:101
本文介绍了如何使用django表单编辑模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的django所以我很抱歉我的新手问题


我有一个模型,我需要让用户使用django形式或任何其他方式编辑数据。



在上图中,我想显示这个表单准备填充数据,并让用户更新它。

最好的方法是什么?



编辑:这里是我的views.py代码

  def exam_Edit(request,examName,number = 0):
numner = int(number)
number = int(number)
questionNo = int(numner)
Myexam = models.Exam.objects.get(name = examName)



QuestionsAll = models.Question.objects.filter(exam = Myexam)

myQeustion = Question.objects.filter(exam = Myexam)[nextQuestion]

answer1 = models.Asnwers.objects.filter(question = myQeustion)[0]
answer2 = models.Asnwers.objects.filter(question = myQeustion)[1]
answer3 = models.Asnwers.objects.filter(question = myQeustion)[2]
answer4 = models.Asnwers.objects.filter(question = myQeustion)[3]


#这里是我的问题:下面的行创建一个带有数据的表单,但不保存到保存对象
form = QuestionsEditForm(initial = {'questionText ':myQeustion .__ unicode __(),'firstChoiceText':answer1 .__ unicode __(),'secondChoiceText':answer2 .__ unicode __(),'thirdChoiceText':answer3 .__ unicode __(),' b
$ b如果request.method =='POST':
#if post

如果form.is_valid():
questionText = form.cleaned_data [' questionText']
Myexam = Exam.objects.get(name = examName)
myQeustion.questionText = form.cleaned_data ['questionText']



answer1.answerText = form.cleaned_data ['firstChoiceText']
answer1.save()

answer2.answerText = form.cleaned_data ['secondChoiceText']
answer2.save )

答案3.answerText = form.cleaned_data ['thirdChoiceText']
answer3.save()

answer4.answerText = form.cleaned_data ['fourthChoiceText']
answer4.save()

variables = RequestContext(request,{'form':form,'examName':examName,'questionNo':str(nextQuestion)})
return render_to_response('exam_edit.html'变量)

请帮助

解决方案

假设您正在使用 ModelForm ,请使用实例关键字参数,并传递您正在更新的模型。



所以,如果你有 MyModel MyModelForm (后者必须扩展 django.forms.ModelForm ),那么您的代码段可能如下所示:

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

然后,用户通过POST发回数据:

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

顺便提一下, ModelForm 的文档在这里: http://docs.djangoproject.com/en/1.8/topics/forms/modelforms/ a>


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.

look at the image above , i want to show this form ready populated with the data and let user update it.
what is the best way to do this ?

EDIT : here is my views.py code

def exam_Edit(request,examName,number=0):
    numner = int(number)
    number = int(number)
    questionNo = int(numner)
    Myexam = models.Exam.objects.get(name = examName)



    QuestionsAll = models.Question.objects.filter(exam = Myexam)

    myQeustion = Question.objects.filter(exam = Myexam)[nextQuestion]

    answer1 =  models.Asnwers.objects.filter(question=myQeustion)[0]
    answer2 =  models.Asnwers.objects.filter(question=myQeustion)[1]
    answer3 =  models.Asnwers.objects.filter(question=myQeustion)[2]
    answer4 = models.Asnwers.objects.filter(question=myQeustion)[3]


    # HERE IS MY PROBLEM : the line below creates a form with a data but it doesn't save it to the save object     
    form = QuestionsEditForm(initial = {'questionText':myQeustion.__unicode__() , 'firstChoiceText':answer1.__unicode__(),'secondChoiceText':answer2.__unicode__(),'thirdChoiceText':answer3.__unicode__(),'forthChoiceText':answer4.__unicode__()})

    if request.method =='POST':
        #if post 

        if form.is_valid():
            questionText = form.cleaned_data['questionText']
            Myexam = Exam.objects.get(name = examName)
            myQeustion.questionText = form.cleaned_data['questionText']



            answer1.answerText = form.cleaned_data['firstChoiceText']
            answer1.save()

            answer2.answerText = form.cleaned_data['secondChoiceText']
            answer2.save()

            answer3.answerText = form.cleaned_data['thirdChoiceText']
            answer3.save()

            answer4.answerText = form.cleaned_data['forthChoiceText']
            answer4.save()

 variables = RequestContext(request,     {'form':form,'examName':examName,'questionNo':str(nextQuestion)})
 return render_to_response('exam_edit.html',variables)               

please help

解决方案

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/

这篇关于如何使用django表单编辑模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆