我是 django 的新手,我已经为这个特定项目奋斗了一段时间。每次尝试提交表单(最终创建 Visitor 和 VisitRequests 对象)时,我都会收到一个非空约束错误。错误来自这行代码...visit_request = VisitRequests(staff=staff, visitor=visitor, comment=comment, token=token, status=None).save()... 请查看下面的代码。
views.py
def formpage(request):
if request.method=='POST':
token=secrets.token_urlsafe(20)
visitor_name=request.POST.get('visitorsname')
comment=request.POST.get('comment')
visit_type=request.POST.get('visit_type')
visit_content='You have a waiting visitor'+'\n'+'Name:'+visitor_name+'\n'+'Purpose Of Visit:'+visit_type+'\n'+'Additional Comment:'+comment+'\n'+token
staff_id=request.POST.get('staff')
staff=Staff.objects.get(id=staff_id)
staff_email=staff.staff_email
req_comment = request.POST.get('req_comment')
request_id = (request.POST.get('request_id'))
visitor=Visitor(visitor_name=visitor_name).save()
visit_request = VisitRequests(staff=staff, visitor=visitor, comment=comment, token=token, status=None).save()
模型.py
class Staff(models.Model):
staff_name = models.CharField(max_length=250)
staff_email = models.CharField(max_length=250, default="")
def __str__(self):
return self.staff_name
class Visitor(models.Model):
visitor_name = models.CharField(max_length=250)
timestamp = models.DateTimeField(default=timezone.now)
def __str__(self):
return '{}'.format(self.visitor_name)
class VisitRequests(models.Model):
staff=models.ForeignKey(Staff, on_delete=models.CASCADE)
visitor = models.ForeignKey(Visitor, on_delete=models.CASCADE)
comment= models.TextField(default='')
status= models.NullBooleanField()
token=models.CharField(max_length=20)
我是一名优秀的程序员,十分优秀!