Python 如何按字段类型筛选查询集
我用夹层。它具有以下模型:Python 如何按字段类型筛选查询集,python,django-queryset,django-1.4,Python,Django Queryset,Django 1.4,我用夹层。它具有以下模型: class AssignedKeyword(Orderable): """ A ``Keyword`` assigned to a model instance. """ keyword = models.ForeignKey("Keyword", related_name="assignments") content_type = models.ForeignKey("contenttypes.ContentType")
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)
我想获得所有唯一的AssignedKeyword模型实例,其中content\u object
字段的类型是Post
。如何按字段类型筛选查询集?答案很简单:
AssignedKeyword.objects.filter(content_type=ContentType.objects.get(name='Product'))