Django 德扬戈。获取相关查询集的并集
以下是我正在尝试做的。Django 德扬戈。获取相关查询集的并集,django,django-queryset,Django,Django Queryset,以下是我正在尝试做的。 当然这会很慢,不知道是否有更好的方法 class Foo(models.Model): bars = generic.GenericRelation(Bar) class Meta: abstract = True class Bar(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveInte
当然这会很慢,不知道是否有更好的方法
class Foo(models.Model):
bars = generic.GenericRelation(Bar)
class Meta:
abstract = True
class Bar(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
invitation = generic.GenericForeignKey('content_type', 'object_id')
timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
meat = models.ForeignKey(Jessy)
bars = Bar.objects.none()
for foo in Foo.objects.all():
bars = bars | Q(foo.bars.all())
bars.values('meat').order_by('timestamp'):
您只需使用:
Bar.objects.filter(content_type=ContentType.objects.get_for_model(Foo), object_id__isnull=False).values('meat').order_by('timestamp')
这将查询与任何Foo
模型关联的所有Bar
对象