我有两个ModelForms从单个视图提交。一个模型是另一个模型的ForeignKey。此时,表单将添加到DB,但不是预期的。我没有添加课程和部分(课程字段填充了新创建的课程),而是获得了一个链接的课程和部分,但是部分名称作为课程名称和部分名称。
models.py
class Course(models.Model):
Name = models.CharField(max_length=30,unique=True)
Active = models.BooleanField(default=True)
def __unicode__(self):
return u'%s' % (self.Name)
class Section(models.Model):
Name = models.CharField(max_length=30,default='.',unique=True)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
assessments = models.ManyToManyField(Assessment)
def __unicode__(self):
return u'%s / %s' % (self.Name, self.course)
forms.py
class CourseAddForm(forms.ModelForm):
class Meta:
model = Course
fields = ['Name', 'Active']
class SectionAddForm(forms.ModelForm):
class Meta:
model = Section
fields = ['Name']
模板/ index.html中
<!-- COURSE ADD MODAL -->
<div class="modal fade" id="CourseAddModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:5px 10px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4>Add Course</h4>
</div>
<div class="modal-body" style="padding:10px 10px;">
<form data-parsley-validate method="post" id="coursesecaddform" action="" enctype="multipart/form-data"
data-parsley-trigger="focusout">
{% csrf_token %}
Add course
{{ courseaddform.as_p }}
Add section
{{ sectionaddform.as_p }}
<p id="login-error"></p>
<input type="submit" class="btn btn-info submit" name="AddCourse" value="Add Course" />
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
views.py
def IndexView(request,Course_id,Section_id):
if request.method == "GET":
this_course = Course.objects.get(pk=Course_id)
active_courses = Course.objects.all().filter(Active=True).exclude(pk=Course_id)
section_list = Section.objects.all().filter(course=this_course)
if len(section_list) >1:
multi_section = True
else:
multi_section = False
active_section = Section.objects.get(pk=Section_id)
roster = Student.objects.all().filter(sections__in=[active_section])
announcement_list = Announcement.objects.all().filter(sections__in=[active_section])
courseaddform = CourseAddForm()
sectionaddform = SectionAddForm()
context = {'active_courses':active_courses, 'this_course': this_course,
'active_section':active_section, 'section_list':section_list,
'roster':roster, 'multi_section':multi_section,
'announcement_list':announcement_list, 'courseaddform':courseaddform,
'sectionaddform':sectionaddform}
return render(request,'gbook/index.html', context)
elif request.method == "POST":
this_course = Course.objects.get(pk=Course_id)
active_courses = Course.objects.all().filter(Active=True).exclude(pk=Course_id)
section_list = Section.objects.all().filter(course=this_course)
if len(section_list) >1:
multi_section = True
else:
multi_section = False
active_section = Section.objects.get(pk=Section_id)
f = CourseAddForm(request.POST, instance=Course())
g = SectionAddForm(request.POST, instance=Section())
if f.is_valid() and g.is_valid():
new_course = f.save()
new_section = g.save(commit=False)
new_section.course = new_course
print new_section.course
new_section.save()
return redirect("/gbook/"+str(Course_id)+"/"+str(active_section))
答案 0 :(得分:1)
解决方案:
使用numpy.random.randn()
:
instance
参数
prefix
答案 1 :(得分:0)
两个模型都有一个def IndexView(request,Course_id,Section_id):
if request.method == "GET":
this_course = Course.objects.get(pk=Course_id)
active_courses = Course.objects.all().filter(Active=True).exclude(pk=Course_id)
section_list = Section.objects.all().filter(course=this_course)
if len(section_list) >1:
multi_section = True
else:
multi_section = False
active_section = Section.objects.get(pk=Section_id)
roster = Student.objects.all().filter(sections__in=[active_section])
announcement_list = Announcement.objects.all().filter(sections__in=[active_section])
courseaddform = CourseAddForm(prefix='crs')
sectionaddform = SectionAddForm(prefix='sctn')
context = {'active_courses':active_courses, 'this_course': this_course,
'active_section':active_section, 'section_list':section_list,
'roster':roster, 'multi_section':multi_section,
'announcement_list':announcement_list, 'courseaddform':courseaddform,
'sectionaddform':sectionaddform}
return render(request,'gbook/index.html', context)
elif request.method == "POST":
this_course = Course.objects.get(pk=Course_id)
active_courses = Course.objects.all().filter(Active=True).exclude(pk=Course_id)
section_list = Section.objects.all().filter(course=this_course)
if len(section_list) >1:
multi_section = True
else:
multi_section = False
active_section = Section.objects.get(pk=Section_id)
f = CourseAddForm(request.POST, prefix='crs')
g = SectionAddForm(request.POST, prefix='sctn')
if f.is_valid() and g.is_valid():
new_course = f.save()
new_section = g.save(commit=False)
new_section.course = new_course
print new_section.course
new_section.save()
return redirect("/gbook/"+str(Course_id)+"/"+str(active_section))
字段,因此两个表单都包含一个具有相同Name
属性的输入。当Django遇到两个具有相同名称的POST参数时,它会将最后一个值绑定到该名称的所有字段。长话短说 - 改变模型中的字段名称,使它们不同。
答案 2 :(得分:0)
由于您的Course
和Section
都有一个名为Name
的属性,我打赌您的表单包含两个属性为id = "id_Name"
和name = "Name"
的字段