Python Django objects.filter问题
我遇到此错误:Python Django objects.filter问题,python,django,object,django-models,Python,Django,Object,Django Models,我遇到此错误:无法分配“[,]”:“Comment.response”必须是“response”实例如何通过匹配申请人和面试id将响应分配给实例? 另外,我只需要objects.filter()所有可能结果中的第一个响应 我的模型如下: class Response(models.Model): video_guid = models.CharField(max_length=32) interview = models.ForeignKey(Interview) applicant
无法分配“[,]”:“Comment.response”必须是“response”实例
如何通过匹配申请人和面试id将响应分配给实例?
另外,我只需要objects.filter()所有可能结果中的第一个响应
我的模型如下:
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的新手:(非常感谢您的帮助!因为您的查询
Response.objects.filter(面试=面试,申请人=申请人)
正在返回列表,其中包含两个响应对象,不能分配给Comment.Response,它是FK to Response
ForeignKey只能将引用(id)存储到其他表/模型的单个记录。作为您的查询Response.objects.filter(面试=面试,申请人=申请人)
正在返回包含两个响应对象的列表,不能将其分配给Comment.Response,它是FK to Response
ForeignKey只能存储对其他表/模型的单个记录的引用(id)。您的问题是过滤器返回多个结果;而不仅仅是“第一个”或“最后一个”(因为您没有指定这样的条件)
第二个问题是,您的模型不允许每个评论都有多个响应
您有几个选择:
调整您的模型以允许每个注释有多个响应。为此,请将response=models.ForeignKey(response)
更改为response=models.manytomy(response)
(请参阅),然后调整视图。首先创建comment
对象,然后创建comment.response.add()
每个响应
为每个响应条目创建多个comment
对象。这可能不太理想;但它可以在不迁移数据库架构的情况下工作
下面是它的外观:
for i in response:
Comment.objects.create(
user = request.user,
applicant = applicant,
interview = interview,
response = i,
comment = text,
created_at = date)
您的模型具有您不需要的冗余字段。由于Comment
与Response
有关系,因此您不需要在Comment
模型中复制Response
中的字段。您可以从中获取相关字段:
c = Comment.objects.get(pk=1)
c.response.interview # interview object
# Get all the comments for where the interview objects primary key is 1
c = Comment.objects.filter(response__interview__pk=1)
r = Response.objects.get(pk=1)
r.comment_set.all() # all comments for this response
在您坐下来编写模型之前,请写下您需要对数据库执行的查询类型。这将帮助您确定需要哪些字段(以及哪些关系)。例如,目前无法获得特定面试的“第一次”或“最后一次”答复(因为答复中没有日期字段
).您遇到的问题是,您的筛选器返回多个结果;而不仅仅是“第一个”或“最后一个”(因为您没有指定这样的条件)
第二个问题是,您的模型不允许每个评论都有多个响应
您有几个选择:
调整您的模型以允许每个注释有多个响应。为此,请将response=models.ForeignKey(response)
更改为response=models.manytomy(response)
(请参阅),然后调整视图。首先创建comment
对象,然后创建comment.response.add()
每个响应
为每个响应条目创建多个comment
对象。这可能不太理想;但它可以在不迁移数据库架构的情况下工作
下面是它的外观:
for i in response:
Comment.objects.create(
user = request.user,
applicant = applicant,
interview = interview,
response = i,
comment = text,
created_at = date)
您的模型具有您不需要的冗余字段。由于Comment
与Response
有关系,因此您不需要在Comment
模型中复制Response
中的字段。您可以从中获取相关字段:
c = Comment.objects.get(pk=1)
c.response.interview # interview object
# Get all the comments for where the interview objects primary key is 1
c = Comment.objects.filter(response__interview__pk=1)
r = Response.objects.get(pk=1)
r.comment_set.all() # all comments for this response
在您坐下来编写模型之前,请写下您需要对数据库执行的查询类型。这将帮助您确定需要哪些字段(以及哪些关系)。例如,目前无法获得特定面试的“第一次”或“最后一次”答复(因为答复中没有日期字段
).我如何从返回的列表中仅选择第一个结果?我尝试了Response.objects.filter(面试=面试,申请人=申请人)[0],但它不起作用。我如何从返回的列表中仅选择第一个结果?我尝试了Response.objects.filter(面试=面试,申请人=申请人)[0]这不起作用。我要补充的是,即使筛选器返回单个响应,它仍然会出错,因为筛选器返回结果的“列表”,即使只有一个响应。我要补充的是,即使筛选器返回单个响应,它仍然会出错,因为筛选器返回“列表”结果,即使只有一个响应。