[英]How can I filter queryset by type of a field
我使用夾層。 它具有模型:
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_object
字段的類型為Post
。 如何按字段類型過濾查詢集?
答案很簡單:
AssignedKeyword.objects.filter(content_type=ContentType.objects.get(name='Product'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.