多对多不保存在django



my-views.py文件:

form=ClassesForm(request.POST(

if form.is_valid():
New_Class =  Classes()
New_Class.semester = Last_Semester
New_Class.year = form.cleaned_data.get('year')
if form.cleaned_data.get('year') >= 6 and form.cleaned_data.get('year') <= 8:
New_Class.school_mode = 1
else:
New_Class.school_mode = 2
New_Class.brunch = form.cleaned_data.get('brunch')
New_Class.subject = form.cleaned_data.get('subject')


New_Class.teacher.set(Staff.objects.filter(uuid=form.cleaned_data.get('teacher').uuid))
New_Class.save()

我的models.py文件:

@reversion.register((类(基本模型(:学期=模型。ForeignKey(Semester,on_delete=models.PROTECT,default=get_last_Semester,verbose_name=("Semester"((school_mode=模型。IntegerField(db_index=True,choices=SCHOOL_MODE,verbose_name=("学校类型"((年份=型号。IntegerField(db_index=True,choices=CLASS_YEARS,verbose_name=("年份"((早午餐=模特。CharField(max_length=2,verbose_name=("Class Brunch"((subject=模型。CharField(最大长度=15,选项=CLASS_SUBJECTS,verbose_name=("主题"((**教师=模特。ManyToManyField(Staff,blank=True,verbose_name=("班主任"((**

class Meta:
unique_together = ('semester', 'year', 'brunch')
ordering = ['semester', 'year', 'brunch', 'subject']
def __unicode__(self):
return "%d %s %s %s / %s" % (self.semester, self.year, self.brunch, self.subject)

auditlog.register(类(

@reversion.register((班级员工(基本模型(:photo=型号。ImageField(upload_to="Staff/",null=True,blank=True,verbose_name=("照片"((user=型号。OneToOneField(User,on_delete=models.PROTECT,db_index=True,verbose_name=("User"((name=型号。CharField(最大长度=30,db_index=True,verbose_name=("名称"((姓=模特。CharField(max_length=30,db_index=True,verbose_name=("姓氏"((id_no=型号。CharField(max_length=15,unique=True,verbose_name=("身份证号码"((出生日期=型号。DateField(verbose_name=("生日"((出生地=模特。CharField(最大长度=30,verbose_name=("出生地"((gender=模特。IntegerField(选项=性别,默认值=无,verbose_name=("性别"((国籍=模特。CharField(选项=国籍,最大长度=20,verbose_name=("国籍"((blood_type=型号。CharField(choices=BLOOD_TYPES,null=True,blank=True,max_length=10,verbose_name=("血型"((phone_home=型号。CharField(max_length=15,null=True,blank=True,verbose_name=("Phone Home"((phone_mobile=型号。CharField(max_length=15,null=True,blank=True,verbose_name=("Phone Mobile"((电子邮件=型号。EmailField(默认值=None,null=True,blank=True,verbose_name=("电子邮件地址"((address=型号。TextField(verbose_name=("地址"((早午餐=模特。ForeignKey(教师早午餐,on_delete=models.PROTECT,verbose_name=("早午餐"((staff_type=型号。ManyToManyField(职员类型,verbose_name=("职员类型"((staff_status=模型。IntegerField(选项=STAFF_STATUS,默认值=1,verbose_name=("员工状态"((additional_information=模型。CharField(max_length=100,null=True,blank=True,verbose_name=("附加信息"((

class Meta:
ordering = ['name', 'surname']
def __unicode__(self):
return "%s %s" % (self.name, self.surname)
def save(self, *args, **kwargs):
self.name = self.name.title()
self.surname = self.surname.upper()
self.user.save()
super(Staff, self).save(*args, **kwargs)

my-views.py文件:

form = ClassesForm(request.POST)
if form.is_valid():
New_Class =  Classes()
New_Class.semester = Last_Semester
New_Class.year = form.cleaned_data.get('year')
if form.cleaned_data.get('year') >= 6 and form.cleaned_data.get('year') <= 8:
New_Class.school_mode = 1
else:
New_Class.school_mode = 2
New_Class.brunch = form.cleaned_data.get('brunch')
New_Class.subject = form.cleaned_data.get('subject')


New_Class.teacher.set(Staff.objects.filter(uuid=form.cleaned_data.get('teacher').uuid))
New_Class.save()


return redirect(reverse('Create-Classes'))

我的型号.py

class Classes(BaseModel):
semester = models.ForeignKey(Semester, on_delete=models.PROTECT,default=get_last_semester, verbose_name=_("Semester"))
school_mode = models.IntegerField(db_index=True, choices=SCHOOL_MODE, verbose_name=_("School Type"))
year = models.IntegerField(db_index=True, choices=CLASS_YEARS, verbose_name=_("Year"))
brunch = models.CharField(max_length=2, verbose_name=_("Class Brunch"))
subject = models.CharField(max_length=15, choices=CLASS_SUBJECTS, verbose_name=_("Subject"))
teacher = models.ManyToManyField(Staff, blank=True, verbose_name=_("Class Teachers"))
class Meta:
unique_together = ('semester', 'year', 'brunch')
ordering = ['semester', 'year', 'brunch', 'subject']
def __unicode__(self):
return "%d %s %s %s / %s" % (self.semester, self.year, self.brunch, self.subject)

class Staff(BaseModel):
photo = models.ImageField(upload_to="Staff/", null=True, blank=True, verbose_name=_("Photo"))
user = models.OneToOneField(User, on_delete=models.PROTECT, db_index=True, verbose_name=_("User"))
name = models.CharField(max_length=30, db_index=True, verbose_name=_("Name"))
surname = models.CharField(max_length=30, db_index=True, verbose_name=_("Surname"))
id_no = models.CharField(max_length=15, unique=True,verbose_name=_("ID Card Number"))
birthdate = models.DateField(verbose_name=_("Birthdate"))
birthplace = models.CharField(max_length=30, verbose_name=_("Birthplace"))
gender = models.IntegerField(choices=GENDERS, default=None, verbose_name=_("Gender"))
nationality = models.CharField(choices=NATIONALITIES, max_length=20, verbose_name=_("Nationality"))
blood_type = models.CharField(choices=BLOOD_TYPES, null=True, blank=True, max_length=10, verbose_name=_("Blood Type"))
phone_home = models.CharField(max_length=15, null=True, blank=True, verbose_name=_("Phone Home"))
phone_mobile = models.CharField(max_length=15, null=True, blank=True, verbose_name=_("Phone Mobile"))
email = models.EmailField(default=None, null=True, blank=True, verbose_name=_("Email Address"))
address = models.TextField(verbose_name=_("Address"))
brunch = models.ForeignKey(TeacherBrunch, on_delete=models.PROTECT, verbose_name=_("Brunch"))
staff_type = models.ManyToManyField(StaffType, verbose_name=_("Staff Type"))
staff_status = models.IntegerField(choices=STAFF_STATUS, default=1, verbose_name=_("Staff Status"))
additional_information = models.CharField(max_length=100, null=True, blank=True, verbose_name=_("Additional Information"))
class Meta:
ordering = ['name', 'surname']
def __unicode__(self):
return "%s %s" % (self.name, self.surname)
def save(self, *args, **kwargs):
self.name = self.name.title()
self.surname = self.surname.upper()
self.user.save()
super(Staff, self).save(*args, **kwargs)

相关内容

  • 没有找到相关文章

最新更新