How to traverse a GenericForeignKey in Django?
问题 I'm using Django v1.9.4 with PostgreSQL 9.2.14 behind. With the following models: from django.db import models from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey from django.contrib.contenttypes.models import ContentType class Foo(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() bar = GenericForeignKey('content_type', 'object_id') class Bar(models.Model): foos = GenericRelation(Foo, related_query_name='bars')