I want to make forms that submit individually these 2 models. Somehow i managed to get views for them. However, i got error saying:
我想制作单独提交这2个模型的表格。不知怎的,我设法为他们获得了意见。但是,我得到错误说:
unbound method full_clean() must be called with MCQuestions instance as first argument (got nothing instead)
必须使用MCQuestions实例作为第一个参数调用未绑定方法full_clean()(没有任何内容)
my details are:
我的详细信息是:
models.py
models.py
class MCQuestions(models.Model):
question = FroalaField(null=True, blank=True)
qcategory = models.ForeignKey(Categories, related_name="MCCategory", blank=True)
class MCAnswers(models.Model):
questionid = models.ForeignKey(MCQuestions, related_name="mc_answer")
a = models.CharField(max_length=50, null=True, blank=True)
z_a = models.BooleanField(default=False)
b = models.CharField(max_length=50, null=True, blank=True)
z_b = models.BooleanField(default=False)
c = models.CharField(max_length=50, null=True, blank=True)
z_c = models.BooleanField(default=False)
d = models.CharField(max_length=50, null=True, blank=True)
z_d = models.BooleanField(default=False)
views.py
views.py
def AddMCQuestions(request, course_id):
if request.method == 'POST':
aform = MCQuestionsForm(request.POST)
bform = MCAnswersForm(request.POST, instance=MCQuestions)
#cat = Categories.objects.filter(cid=course_id)
aform_valid = aform.is_valid()
**bform_valid = bform.is_valid()**
if aform_valid and bform_valid:
a = aform.save(commit=False)
a.qcategory_id = course_id
a.save()
b = bform.save(commit=False)
b.save()
return HttpResponse('question added')
else:
aform = MCQuestionsForm()
bform = MCAnswersForm()
bform.qcategoryid = a
return render(request, 'teacher/mcquestionadd.html', {'aform': aform, 'bform': bform})
and error is highlighting bform_valid = bform.is_valid()
错误突出显示bform_valid = bform.is_valid()
1 个解决方案
#1
1
This:
这个:
bform = MCAnswersForm(request.POST, instance=MCQuestions)
should be:
应该:
bform = MCAnswersForm(request.POST, instance=answers)
where answers
is an instance of MCAnswers
. There is no answer yet in your view (you are about to create it) so just remove it from your code for now.
其中答案是MCAnswers的一个实例。您的视图中还没有答案(您即将创建它),因此暂时将其从代码中删除。
You will need to add prefixes to your forms so the validation will work properly and you better give meaningful names to these (not aform
)
您需要在表单中添加前缀,以便验证能够正常运行,并且您最好为这些表单赋予有意义的名称(不是aform)
question_form = MCQuestionsForm(request.POST, prefix="question")
answer_form = MCAnswersForm(request.POST, prefix="answers")
Finally to save, do this - you are missing the link from the question to the answer:
最后保存,这样做 - 你错过了从问题到答案的链接:
if question_form.is_valid() and answer_form.is_valid():
question = question_form.save(commit=False)
question.qcategory_id = course_id
question.save()
answer = bform.save(commit=False)
answer.question = question
answer.save()
return HttpRedirect(reverse('...'))
#1
1
This:
这个:
bform = MCAnswersForm(request.POST, instance=MCQuestions)
should be:
应该:
bform = MCAnswersForm(request.POST, instance=answers)
where answers
is an instance of MCAnswers
. There is no answer yet in your view (you are about to create it) so just remove it from your code for now.
其中答案是MCAnswers的一个实例。您的视图中还没有答案(您即将创建它),因此暂时将其从代码中删除。
You will need to add prefixes to your forms so the validation will work properly and you better give meaningful names to these (not aform
)
您需要在表单中添加前缀,以便验证能够正常运行,并且您最好为这些表单赋予有意义的名称(不是aform)
question_form = MCQuestionsForm(request.POST, prefix="question")
answer_form = MCAnswersForm(request.POST, prefix="answers")
Finally to save, do this - you are missing the link from the question to the answer:
最后保存,这样做 - 你错过了从问题到答案的链接:
if question_form.is_valid() and answer_form.is_valid():
question = question_form.save(commit=False)
question.qcategory_id = course_id
question.save()
answer = bform.save(commit=False)
answer.question = question
answer.save()
return HttpRedirect(reverse('...'))