【问题标题】:django combine queries on two different base modelsdjango 在两个不同的基本模型上组合查询
【发布时间】:2014-03-17 15:10:47
【问题描述】:

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

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

这个功能我试过了

查询集 = (q1) | (q2)

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

所以我尝试了其他两个函数,但这些函数也抛出相同的异常“列表”对象没有属性“模型”

查询集 = 列表(q1)+ 列表(q2)

查询集 = 链(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)

我该怎么做?

【问题讨论】:

    标签: python django django-queryset


    【解决方案1】:

    由于 django 不支持混合查询集(它们必须只对应一个模型),我建议您使用不同的方法:模型继承或 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 2:一旦查询属于同一模型(例如 q1 + q2),您可以将它们与您之前尝试过的方法合并

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 2010-09-23
      • 2018-04-24
      • 2011-03-19
      • 2011-10-07
      • 2018-06-27
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多