我是新的django,我有三种模式:学生建议,员工建议和任命,当学生建议和员工建议的状态字段之间的其中一项获得批准时,数据将被插入到约会模型中
在我的模型中。py:
statuses=[
('accepted','Approve'),
('denied','deny'),
('canceled','cancel'),
]
class studentsuggestion(models.Model):
student = models.ForeignKey(student, default=None, on_delete=models.CASCADE)
lecture = models.ForeignKey(staffs, on_delete=models.CASCADE)
suggestion_date = models.DateField(default=None,blank=True)
suggestion_hour = models.TimeField(default=None,null=True, blank=True)
student_reason = models.TextField(max_length=1000, blank=True)
status = models.CharField(max_length=9, default=None, choices=statuses, null=True, blank=True)
suggested_date = models.DateTimeField(auto_now_add=True)
objects = studentsuggstmanager()
def __str__(self):
return self.student.user.username
def get_absolute_url(self):
return reverse('suggestion_detail', kwargs = {'pk':self.pk})
class staffsuggestion(models.Model):
auhtor = models.ForeignKey(staffs, related_name='stasuggestion', default=None, on_delete=models.CASCADE)
studentsuggestion = models.ForeignKey(studentsuggestion, default=None, on_delete=models.CASCADE)
suggestion_date = models.DateField(default=None,blank=True)
suggestion_hour = models.TimeField(default=None,null=True, blank=True)
end_time = models.TimeField(default=None,null=True, blank=True)
staff_reason = models.TextField(max_length=1000, blank=True)
status = models.CharField(max_length=9, default=None, choices=statuses, null=True, blank=True)
suggested_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.studentsuggestion
class appointment(models.Model):
staffsuggestion = models.ForeignKey(staffsuggestion, default=None, null=True, on_delete=models.CASCADE)
studentsuggestion = models.ForeignKey(studentsuggestion, default=None,null=True, on_delete=models.CASCADE)
suggestion_date = models.DateField(default=None,blank=True)
suggestion_hour = models.TimeField(default=None,null=True, blank=True)
end_time = models.TimeField(default=None,null=True, blank=True)
reason = models.TextField(max_length=1000)
status = models.CharField(max_length=9, default=None, null=True, choices=statuses, blank=True)
requested_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
if staffsuggestion is not None:
return self.staffsuggestion
else:
return self.studentsuggestion
在views.py中:
class studentsuggestionreact(UpdateView):
model = studentsuggestion
template_name = 'auth/mine/reaction.html'
fields = ['status']
def form_valid(self, form):
self.object = form.save(commit = False)
if self.object.status == 'approve':
studentsuggestion = self.object.student
suggestion_date = self.object.suggestion_date
suggestion_hour = self.object.suggestion_hour
reason = self.object.student_reason
status = self.object.status
get_values = appointment.objects.get_or_create( studentsuggestion=studentsuggestion,
suggestion_date=suggestion_date, suggestion_hour=suggestion_hour, reason=reason,
status=status).save()
get_values.save()
self.object.save()
return redirect("suggestion_list")
elif self.object.status == 'canceled':
self.object.save()
return redirect("suggestion_list")
elif self.object.status == 'denied':
self.object.save()
return redirect("suggestion_list")
我如何更新该约会模型字段?