GqlQuery object and JSON

28 views
Skip to first unread message

Sathish Kumar Parthasarathy

unread,
Apr 18, 2008, 3:17:48 PM4/18/08
to Google App Engine
Hi, Can Any tell me how to convert GqlQuery object to JSON format
i use serializers form django.core its not working its throws an
error

'Marker' object has no attribute '_meta'

marker_list = db.GqlQuery('SELECT * FROM Marker')

jsonmarker = serializers.serialize("json", marker_list)

bye
Duke

Jeff Hinrichs

unread,
Apr 18, 2008, 7:29:11 PM4/18/08
to Google App Engine
if serializers.serialize is looking for marker_list to be an iterable,
it won't work like that as marker_list is just `db.GqlQuery('SELECT *
FROM Marker')`
Try:
jsonmarker = serializers.serialize("json", marker_list.all())

Regards,

Jeff
> bye
> Duke

Sathish Kumar Parthasarathy

unread,
Apr 18, 2008, 9:01:52 PM4/18/08
to Google App Engine

that is not working
Thank for the replay
Regards
Duke

Sathish Kumar Parthasarathy

unread,
Apr 18, 2008, 9:51:39 PM4/18/08
to Google App Engine
How to use simplejson with GqlQuary object for example how to convert
marker_list to JSON for format

marker_list = db.GqlQuery('SELECT * FROM Marker')

Thanks
Duke

limodou

unread,
Apr 18, 2008, 9:57:47 PM4/18/08
to google-a...@googlegroups.com
I think you should dump queryset or row to python data, then convert
them to json format, here is my testing code:

def dumprow(r):
import datetime
a = {}
for k in r.fields().keys():
v = getattr(r, k)
a['id'] = r.key().id()
if isinstance(v, str):
a[k] = str(v)
elif isinstance(v, unicode):
a[k] = unicode(v)
elif isinstance(v, datetime.datetime):
a[k] = v.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(v, datetime.date):
a[k] = v.strftime('%Y-%m-%d')
elif isinstance(v, datetime.time):
a[k] = v.strftime('%H:%M:%S')
elif isinstance(v, (int, float, list)):
a[k] = v
else:
a[k] = str(v)
return a

def dumpquery(query):
s = []
for r in query:
s.append(dumprow(r))
return s

then you can use simplejson to convert them to json format.

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou

Sathish Kumar Parthasarathy

unread,
Apr 19, 2008, 2:02:37 AM4/19/08
to Google App Engine

Thank a lot
The code Works fine

Duke

limodou

unread,
Apr 19, 2008, 2:12:39 AM4/19/08
to google-a...@googlegroups.com
On Sat, Apr 19, 2008 at 2:02 PM, Duke <p.sath...@gmail.com> wrote:
>
>
> Thank a lot
> The code Works fine
>
Maybe you can also add this line:

a['key'] = str(r.key())

Reply all
Reply to author
Forward
0 new messages