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
Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.
The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.
Django Model FormIt is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With