我得到这个错误:Cannot assign "[<Response: Response object>, <Response: Response object>]": "Comment.response" must be a "Response" instance
。如何通过匹配申请人和面试 ID 为实例分配响应?另外,我只想要 objects.filter() 的所有可能结果中的第一个响应
def post_comment(request, interview_id, applicant_id):
if request.POST:
text = str(request.POST['add_comment'])
interview = Interview.objects.get(id = interview_id)
applicant = Applicant.objects.get(id = applicant_id)
response = Response.objects.filter(interview = interview, applicant = applicant)
date = datetime.datetime.now()
comment = Comment(
user = request.user,
applicant = applicant,
interview = interview,
response = response,
comment = text,
created_at = date,
)
我的模型如下:
class Response(models.Model):
video_guid = models.CharField(max_length=32)
interview = models.ForeignKey(Interview)
applicant = models.ForeignKey(Applicant)
question = models.ForeignKey(Question)
class Comment(models.Model):
user = models.ForeignKey(User)
applicant = models.ForeignKey(Applicant)
interview = models.ForeignKey(Interview)
response = models.ForeignKey(Response)
comment = models.TextField(default='')
created_at = models.DateTimeField(default=datetime.datetime.now())
我是 Django 的新手 :( 非常感谢任何帮助!