【问题标题】:How can I filter queryset by type of a field如何按字段类型过滤查询集
【发布时间】:2013-02-26 14:50:50
【问题描述】:

我使用夹层。它有型号:

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。如何按字段类型过滤查询集?

【问题讨论】:

    标签: python django-queryset django-1.4


    【解决方案1】:

    答案很简单:

    AssignedKeyword.objects.filter(content_type=ContentType.objects.get(name='Product'))
    

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2023-01-04
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多