I have three different types of objects: RawArticle
, RawPatent
and RawGrant
.
I have great working serializers, whose base class is serializers.ModelSerializer
.
If I retrieve a query set of RawArticle
I can pass that directly to the serializer with many=True
and the output is nominal:
[{"save_link": "...", "published": "2014-01-18T20:39:54.086Z", }, {"save_link": "...", "published": "..."}, ...]
This response is generated from the following code:
return Response(RawArticleSerializer(articles, many=True).data)
Now that I have three different objects, I'd like to chain them just as above but with their respective objects.
I have been unsuccessful so far. My initial idea was to simply create a list of the serialized objects and return that (serialized of course), as such:
all_latest = user_latest(request)['latest_articles']
available_serializers = {RawArticle: RawArticleSerializer, RawGrant: RawGrantSerializer, RawPatent: RawPatentSerializer}
serialized = []
for article in all_latest:
serialized.append((available_serializers[type(article)](article, many=False)).data)
return Response(str(serialized))
The above code does not return JSON and the date time aren't converted to actual date and time as string.
My latest attempt was using json.dumps and simplejson and both raise a TypeError
saying:
date time object is not serializable.
Any hint would be greatly appreciated.