我正在尝试保存一个包含多个字段的表单,例如多对一关系和MultiSelectCheckBox,如下所示:
型号:
class master(models.Model):
id_numb=models.CharField(max_length=40,unique=True)
gender=models.CharField(max_length=6)
name=models.CharField(max_length=100)
family_name=models.CharField(max_length=100)
birth_date=models.DateField()
nation_id=models.IntegerField(blank=True, null=True)
id_code=models.IntegerField(blank=True, null=True)
education=models.CharField(max_length=100, blank=True, null=True)
job=models.CharField(max_length=100, blank=True, null=True)
marriage_state=models.CharField(max_length=6, blank=True, null=True)
comm=models.CharField(max_length=100, blank=True, null=True)
mobile=models.IntegerField(blank=True, null=True)
phone=models.IntegerField(blank=True, null=True)
email=models.EmailField(blank=True, null=True)
address=models.CharField(max_length=200, blank=True, null=True)
def __str__(self):
return (self.name +" "+ self.family_name)
class kelas(models.Model):
master=models.ForeignKey(master,on_delete=models.CASCADE,blank=True, null=True)
kelas_no=models.CharField(max_length=40,unique=True)
start_date_c=models.DateField(blank=True,null=True)
finish_date_c=models.DateField(blank=True,null=True)
day_no=models.CharField(max_length=200,blank=True,null=True)
查看:
def courseregister(request):
if request.method=="POST":
form=CourseForm(request.POST)
courses=kelas.objects.all()
if form.is_valid():
try:
form.save()
form=CourseForm()
#return redirect("/show")
except:
pass
else:
form=CourseForm()
courses=kelas.objects.all()
return render(request, 'music/course.html', {'form':form,'courses':courses})
表格:
class CourseForm(forms.ModelForm):
days=(
('1','Sa'),
('2','Su'),
('3','Mo'),
('4','Tu'),
('5','We'),
('6','Th'),
('7','Fr'),
)
day_no=forms.ChoiceField(choices=days, widget=forms.CheckboxSelectMultiple())
class Meta:
model = kelas
fields = "__all__"
def __init__(self, *args, **kwargs):
super(CourseForm, self).__init__(*args, **kwargs)
for field in iter(self.fields):
self.fields[field].widget.attrs.update({
'class': 'form-control myinputbox'
})
self.fields['day_no'].widget.attrs.update({'class': 'form-check-input'})
模型“ Kelas”中的数据未保存。 我的错在哪里? 我复查了很多次,但是我什么都没找到。 预先感谢您的帮助。