我正在尝试将sql查询转换为django查询但未能做到这一点,任何人都可以帮助我
select id,name,round(value::numeric,2) as value, st_transform(geometry, 3857) as geometry
from net_rest
where state_id in (1,2) and name = 'height'
union
(select d.id,d.restriction,d.value, st_transform(d.geometry, 3857) as geometry from display_rest d
where d.restriction='height' and condition_id not in (select condition_id
from net_rest_conditions where version_id = 2)
或者这个
select id,name,value as value, geometry
from net_rest
where state_id in (1,2) and name = 'height'
union
(select d.id,d.restriction,d.value,geometry from display_rest d
where d.restriction='height' and condition_id not in (select condition_id
from net_rest_conditions where version_id = 2)
更新了此处的问题
我正在使用django django rest framework serialize net_rest 模型,基本上我正在研究与GIS相关的项目,我必须使用rest api来公开数据
以下是我的一些模特
class net_rest(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_net_rest'
def __unicode__(self):
return '%s' % self.name
class display_rest(models.Model):
restriction = models.CharField(max_length=45, blank=True)
link_id = models.IntegerField(blank=True, null=True)
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_display_rest'
class net_rest_conditions(models.Model):
condition_id = models.IntegerField()
version_id = models.IntegerField(blank=True, null=True)
class Meta:
db_table = u'tbl_net_rest_conditions'
class networkSerializer(serializers.GeoModelSerializer):
class Meta:
model = net_rest
fields = ('id', 'name', 'value', 'geometry')
这是视图
class networkSerializerViewSet(viewsets.ModelViewSet):
q1 = display_rest.objects.values_list('id', 'name', 'value', 'geometry').filter(restriction='height')\
.exclude(condition_id__in=net_rest_conditions.objects.filter(version_id=2).values_list('condition_id',flat=True))
q2 = net_rest.objects.all().filter(Q(name="height"), Q(state_id=1) | Q(state_id=2)).values_list('id', 'value', 'geometry')
queryset = q1 | q2
serializer_class = networkSerializer
答案 0 :(得分:0)
django中无法进行更复杂的查询,您可以使用原始查询。
<OBJECT>.objects.raw('sql statement goes here')