Hello, I want flask methods to return either a page with an HTML table of the object elements returned by query or JSON/XML data for this object elements if visitor specify the format. What is a good way to return JSON representation of SQLAlchemy result collection / pagination in Flask? Django has something like http://docs.djangoproject.com/en/dev/topics/serialization/ Thanks, Alex
Something like this:
return jsonify(username=g.user.username,
email=g.user.email,
id=g.user.id)
http://flask.pocoo.org/docs/api/#returning-json
On Wed, Feb 16, 2011 at 3:15 PM, Alex <personxxl@gmail.com> wrote:
> Hello,
>
> I want flask methods to return either a page with an HTML table of the
> object elements returned by query or JSON/XML data for this object elements
> if visitor specify the format.
>
> What is a good way to return JSON representation of SQLAlchemy result
> collection / pagination in Flask?
>
> Django has something like
> http://docs.djangoproject.com/en/dev/topics/serialization/
>
> Thanks,
> Alex
>
>
>
--
freelance web development
aaron.kavlie.net
This is the first thing I tried - but it serializes one object, but how to
jsonify collection / pagination result?
I'm not sure how to use jsonify() method to serialize such structure:
result:
page_num
amount_of_results
result_collection[
res1(a, b, c),
res2(a, b, c),
....,
resX(a, b, c)
]
On Wed, Feb 16, 2011 at 5:18 PM, Aaron Kavlie <akavlie@gmail.com> wrote:
> Something like this:
>
> return jsonify(username=g.user.username,
> email=g.user.email,
> id=g.user.id)
>
> http://flask.pocoo.org/docs/api/#returning-json
>
> On Wed, Feb 16, 2011 at 3:15 PM, Alex <personxxl@gmail.com> wrote:
>
>> Hello,
>>
>> I want flask methods to return either a page with an HTML table of the
>> object elements returned by query or JSON/XML data for this object elements
>> if visitor specify the format.
>>
>> What is a good way to return JSON representation of SQLAlchemy result
>> collection / pagination in Flask?
>>
>> Django has something like
>> http://docs.djangoproject.com/en/dev/topics/serialization/
>>
>> Thanks,
>> Alex
>>
>>
>>
>
>
> --
> freelance web development
> aaron.kavlie.net
>
Jsonify takes the same parameters as a dictionary, so why not just
pass it a dictionary?
dict = {}
dict["page_num"] = result.page_num
dict["amount_of_results"] = result.amount_of_results
for row in result_collection:
dict[row.id] = row
or if row is not in the right format, then do the same for it's attributes.
On Wed, Feb 16, 2011 at 5:40 PM, Alex <personxxl@gmail.com> wrote:
> This is the first thing I tried - but it serializes one object, but how to
> jsonify collection / pagination result?
> I'm not sure how to use jsonify() method to serialize such structure:
> result:
> page_num
> amount_of_results
> result_collection[
> res1(a, b, c),
> res2(a, b, c),
> ....,
> resX(a, b, c)
> ]
>
>
> On Wed, Feb 16, 2011 at 5:18 PM, Aaron Kavlie <akavlie@gmail.com> wrote:
>>
>> Something like this:
>>
>> return jsonify(username=g.user.username,
>> email=g.user.email,
>> id=g.user.id)
>>
>> http://flask.pocoo.org/docs/api/#returning-json
>> On Wed, Feb 16, 2011 at 3:15 PM, Alex <personxxl@gmail.com> wrote:
>>>
>>> Hello,
>>> I want flask methods to return either a page with an HTML table of the
>>> object elements returned by query or JSON/XML data for this object elements
>>> if visitor specify the format.
>>> What is a good way to return JSON representation of SQLAlchemy result
>>> collection / pagination in Flask?
>>> Django has something
>>> like http://docs.djangoproject.com/en/dev/topics/serialization/
>>> Thanks,
>>> Alex
>>>
>>
>>
>>
>> --
>> freelance web development
>> aaron.kavlie.net
>
>
--
GPG Key ID: C92EF367 / 1428 FE8E 1E07 DDA8 EFD7 195F DCCD F5B3 C92E F367
WWW: http://www.sharms.org/blog
Hi, On 2/16/11 11:40 PM, Alex wrote: > This is the first thing I tried - but it serializes one object, but how > to jsonify collection / pagination result? By putting it into the object: return jsonify(items=list_of_items) For more information why only objects are supported: http://flask.pocoo.org/docs/security/#json-security Regards, Armin