i have two models with relationship one to many in django app using app using postgres/postgis database. i have create one not easy query in database and pgadmin panel and works correct.
here the query :
select string_agg(distinct app_work.id::text, ', ') AS code_work,string_agg(distinct app_work.stage, ', ')
AS stage,string_agg(distinct app_work.dfield_work, ', ') AS dfield,app_point.geom
from app_point, app_work where app_point.id=app_work.point_field_id GROUP BY app_point.id;
now i want to use this query(i need that results from this query) in my django app to create a geojson or json export.
i am not sure how convert this query using django method objects and queries(like point.objects.all()) i try to use custom postgres query like this :
models.py
class point(models.Model):
geom = models.MultiPointField(srid=4326)
objects = models.GeoManager()
def __unicode__(self):
return str(self.id)
class meta:
verbose_name_plural="point_info"
class work(models.Model):
dfield_work = models.CharField(max_length=100,blank=True, null=True)
stage = models.CharField(max_length=100,blank=True, null=True)
field1 = models.CharField(max_length=100,blank=True, null=True)
field2 = models.CharField(max_length=100,blank=True, null=True)
point_field= models.ForeignKey('point', blank=True, null=True)
def __unicode__(self):
return str(self.id)
vews.py
from django.db import connection
def points(request):
cursor = connection.cursor()
cursor.execute("""seselect string_agg(distinct app_work.id::text, ', ') AS code_work,string_agg(distinct app_work.stage, ', ')
AS stage,string_agg(distinct app_work.dfield_work, ', ') AS dfield,app_point.geom
from app_point, app_work where app_point.id=app_work.point_field_id GROUP BY app_point.id from log_point, log_work where log_point.id=log_work.point_field_id GROUP BY log_point.id""")
row = cursor.fetchall()
print row
data = serialize('geojson', row)
return HttpResponse(data,content_type='json')
in the print row i take a correct list results
but not working and i have this error :
'tuple' object has no attribute '_meta'
any idea how to fix it ?
serializedefined?