Python Django:无法解析泛型关系上的关键字
我想按用户筛选我的通用关系,即Python Django:无法解析泛型关系上的关键字,python,django,django-models,Python,Django,Django Models,我想按用户筛选我的通用关系,即 Entry.objects.filter(content_object__user=request.user) 但我得到的信息是: 无法将关键字“content\u object”解析到字段中。选择包括:内容类型、id、对象id、原因、请求 class Unsubscribe(models.Model): """ Notes: See: http://www.screamingatmyscreen.com/2012/6/django-a
Entry.objects.filter(content_object__user=request.user)
但我得到的信息是:
无法将关键字“content\u object”解析到字段中。选择包括:内容类型、id、对象id、原因、请求
class Unsubscribe(models.Model):
"""
Notes:
See: http://www.screamingatmyscreen.com/2012/6/django-and-generic-relations/
"""
content_type = models.ForeignKey(ContentType, help_text="Represents the name of the model")
object_id = models.PositiveIntegerField(help_text="stores the object id")
content_object = generic.GenericForeignKey('content_type', 'object_id')
reason = models.CharField(max_length=60)
request_made = models.DateTimeField(auto_now_add=True,
help_text="Shows when object was created.")
更新:
我发现这句话:
记得我说过,外键和外键之间有一点区别
和通用外键。您不能在查询中使用它们。
Entry.objects.filter(内容对象…)不可能。你可以用
除内容外的所有其他内容
所以这是说,我不可能得到像外键一样的x场
我能看到的唯一解决方案是这样的,但请继续
tmpl['mydict'] = []
for item in Entry.objects.all():
if request.user in item.content_object.user:
tmpl['mydict'].append(item)
然后返回tmpl上下文,而不是原始查询集。这样,tmpl只有该用户的对象列表。您想要实现什么
GenericForeignKey
引用的是没有用户类型的content\u type
。另外,GenericForeignKey
仅在您有实例时帮助您检索对象。不用于Filteringher是此@karthikr I的票证GenericForeignKey引用的所有相关对象都有一个名为user的字段,我只想对该用户进行筛选。例如,联系用户或客户__user@karthikr更新问题:)您不能执行请求.user.entry\u set.all()
?