django在两个不同的基本模型上组合查询 [英] django combine queries on two different base models

查看:2013
本文介绍了django在两个不同的基本模型上组合查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的查询集,我想使两个查询集合的并发

I have two different different queryset, I want to make union of both 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)

queryset = (q1) | (q2)

面对这个错误'django在两个不同的基本模型上组合查询

facing this error ' django combine queries on two different base models '

所以我已经尝试了两个其他的功能,但这些也抛出相同的异常'list'对象没有属性'model''

So i have tried two other functions but these are also throwing same exception ' list' object has no attribute 'model' '


queryset = list(q1)+ list(q2)

queryset = list(q1) + list(q2)

queryset = chain(q1,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

但是这两个函数都没有返回查询类型记录。

But both these functions did not return queryset type record.

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

I want queryset type of tbl_network (q2)

我如何做到这一点?

推荐答案

由于django不支持混合查询必须对应于一个模型),我建议您使用不同的方法:模型继承或FK。

As django doesn't support mixed querysets (they must correspond to just one model), I suggest you using a different approach: a model inheritance or FK.

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))

编辑:不知道你在哪里得到你的限制字段,因为它不会出现在模型中,但是我反正把它尊重你的代码没有错误)

edit: don't know where did you get your 'restriction' field, since it does not appear in models, but i put it anyway to "respect" your code (assuming there's no error there).

编辑2 :您可以将它们与您之前尝试的方法合并,一旦查询是相同的模型(如q1 + q2)

edit 2: you can merge them with the methods you tried before, once the queries are of the same model (e.g. q1 + q2)

这篇关于django在两个不同的基本模型上组合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆