[英]How can I filter queryset by type of a field
I use mezzanine. 我使用夹层。 It has model:
它具有模型:
class AssignedKeyword(Orderable):
"""
A ``Keyword`` assigned to a model instance.
"""
keyword = models.ForeignKey("Keyword", related_name="assignments")
content_type = models.ForeignKey("contenttypes.ContentType")
object_pk = models.IntegerField()
content_object = GenericForeignKey("content_type", "object_pk")
class Meta:
order_with_respect_to = "content_object"
def __unicode__(self):
return unicode(self.keyword)
I want to get all unique AssignedKeyword model instances, where type of content_object
field is Post
. 我想获取所有唯一的AssignedKeyword模型实例,其中
content_object
字段的类型为Post
。 How can I filter queryset by type of a field? 如何按字段类型过滤查询集?
答案很简单:
AssignedKeyword.objects.filter(content_type=ContentType.objects.get(name='Product'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.