Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 德扬戈。获取相关查询集的并集_Django_Django Queryset - Fatal编程技术网

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
对象