我想用ManyToMany
关系保存对象。当我提交表单时,除了有ManyToMany
关系的字段之外,所有的东西都保存了。
这些是我的文件:
#Forms.py
class ExamForm(ModelForm):
class Meta:
model = Exam
fields = '__all__'
#Models.py
class Exam(models.Model):
questions = models.ManyToManyField(Question)
title = models.CharField(max_length=250)
class Question(models.Model):
title = models.CharField(max_length=250)
answer = models.TextField(null=True, blank=True)
#Views.py
def add_exam(request):
if request.method == "POST":
form = ExamForm(request.POST)
if form.is_valid():
new_exam = form.save(commit=False)
new_exam.save()
return redirect('view_exam')
else:
form = ExamForm()
template = 'add_exam.html'
context = {'form': form}
return render(request, template, context)
这些代码有什么问题?