Returned objects in "resource methods" are not serialized correctly

Issue #34 resolved
Seph Soliman
created an issue

If you override a handlers way to return a field then the method cannot return objects due to a "Unable to serialize <MyObject>" error. Example:



class TagHandler(AnonymousBaseHandler): model = Tag fields = ('name',) #(...)

class PhotoHandler(AnonymousBaseHandler): model = Photo fields = ('id', 'title', 'description', 'original', 'thumbnails', 'tags') allowed_methods = ('GET',)

def tags(cls, photo):
    t = Tag() = 'foo'
    return t

def read(self, request, photo_id):
    return { 'photo': p } # this will raise an exception, since Tag cannot be serialized


The specific error in the case above is: {{{


TypeError at /api/photo/1 <Tag: foo> is not JSON serializable }}}

But if I instead do remove the tags resource method and replace it by overriding the local object in the photohandler, it works. Like this:



class PhotoHandler(AnonymousBaseHandler): def read(self, request, photo_id): p = Photo.objects.get(id=photo_id) p.tags = p._get_tags() return { 'photo': p } }}}

Thus it seems that values returned from resource methods aren't serialized properly before being passed on to simplejson.

Comments (2)

  1. Seph Soliman reporter

    It should be noted that the _get_tags() method returns a queryset of Tag objects. Doing _get_tags() in the resource method was of course the first thing I tried but with the same error as described.

  2. Log in to comment