Tuesday, March 18, 2014

How to convert SQL Complex QUERY to django QUERY

I am trying to convert sql query to django query but failed to do this, can anyone help me

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)

OR This

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)

Updated the question fro here

I am using django django rest framework serialize net_rest Model, basically i am working on project related to GIS where i have to make rest api to expose data

Here is some of my models

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

Here is view

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6e137e70-e88d-49bb-b000-e6174aae3837%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment