简体   繁体   中英

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 . How can I filter queryset by type of a field?

答案很简单:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM