Wednesday, July 24, 2013

Add extra object to Tastypie returned json results

It's really hard to find a straight answer on the Internet. Both page1 and page2 point me to the right direction. Adding alter_list_data_to_serialize(self, request, data) function could make it work.

def alter_list_data_to_serialize(self, request, data):
    # Example iteration through data
    for item in data['object']:
        # Add extra object to it
        item.data['extra'] = 'data you want to add'
    # Return data
    return data
Be caution if you try to add extra data like this: item['extra'] = 'data you want to add', you will get error: "Bundle" objects does not support item assignment. Since item is a bundle type object, you should use Bundle.data to get access to it.

You may curious why don't I use dehydrate(self, bundle) function to do that. Because I don't know how to iterate through the bundle object.

No comments:

Post a Comment

Disqus for blog