Django合并两个不同基模型的查询

3 投票
1 回答
4244 浏览
提问于 2025-04-17 22:40

我有两个不同的查询集(queryset),我想把这两个查询集合并在一起。

 q1 = tbl_nt_123.objects.values_list('id', 'value', 'geometry').filter(restriction='height')\
        .exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True))

 q2 = tbl_network.objects.all().filter(Q(name="height"), Q(state_id=1) | Q(state_id=2)).values_list('id', 'value', 'geometry');

我试过这个方法:

queryset = (q1) | (q2)

但是遇到了这个错误:' django combine queries on two different base models ',意思是说在两个不同的基础模型上合并查询。

所以我又尝试了另外两个方法,但它们也出现了同样的错误:' list' object has no attribute 'model ',意思是说列表对象没有'model'这个属性。

queryset = list(q1) + list(q2)

queryset = chain(q1, q2)

在这里更新我的问题

class tbl_nt_123(models.Model):
    condition_id = models.IntegerField(blank=True, null=True)
    value = models.FloatField(blank=True, null=True)
    geometry = models.GeometryField(blank=True, null=True)
    class Meta:
        db_table = u'tbl_nt_123'

class tbl_conditions(models.Model):
    condition_id = models.IntegerField()
    version_id = models.ForeignKey(LookupNavteqversion)
    class Meta:
        db_table = u'tbl_conditions'


class tbl_network(models.Model):
    name = models.CharField(max_length=50, blank=True)
    value = models.FloatField()
    state_id = models.IntegerField(null=True, blank=True)
    geometry = models.GeometryField(null=True, blank=True)
    objects = models.GeoManager()

    class Meta:
        db_table = u'tbl_network'

    def __unicode__(self):
        return '%s' % self.name

class tbl_networkSerializer(serializers.GeoModelSerializer):
    class Meta:
        model = tbl_network
        fields = ('id', 'value', 'geometry')

这是视图文件的内容:

   class NetworkViewSet(viewsets.ModelViewSet):

      q1 = tbl_nt_123.objects.values_list('id', 'value', 'geometry').filter(restriction='height')\
            .exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True))

     q2 = tbl_network.objects.all().filter(Q(name="height"), Q(state_id=1) | Q(state_id=2)).values_list('id', 'value', 'geometry');

     queryset = list(chain(q1, q2))

     serializer_class = tbl_networkSerializer

但是这两个方法都没有返回查询集类型的记录。

我想要的是tbl_network(q2)类型的查询集。

我该怎么做呢?

1 个回答

1

由于Django不支持混合查询集(查询集只能对应一个模型),我建议你换个方法:使用模型继承或者外键。

class MyGeometry(models.Model):
    value = ...
    geometry = ...

class tbl_nt_123(MyGeometry):
    condition_id = models.IntegerField(blank=True, null=True)
    value = models.FloatField(blank=True, null=True)
    geometry = models.GeometryField(blank=True, null=True)

class tbl_network(MyGeometry):
    name = models.CharField(max_length=50, blank=True)
    state_id = models.IntegerField(null=True, blank=True)
    objects = models.GeoManager()

...
#you should also rewrite your queries and then merge them.
#remember that inheritance let's you access parent_links and children_links
#between models.
#your query 1 should be like this one below, and a similar approach for query2.
#since they will be of the same type, you can merge them. Later, you can access
#their children links ('tbl_nt_123' and 'tbl_network), being aware that upon non-existance those links will throw DoesNotExist, upon model iteration.

q1 = MyGeometry.objects.values_list('id', 'value', 'geometry').select_related('tbl_nt_123').filter(tbl_nt_123__restriction='height')\
.exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True))

补充说明:我不知道你提到的'restriction'字段是从哪里来的,因为在模型中没有看到它,不过我还是把它放上去,以“尊重”你的代码(假设那里没有错误)。

补充说明 2:如果查询都是同一个模型的,你可以用之前尝试过的方法把它们合并起来(比如q1 + q2)。

撰写回答