Reputation: 66320
this is in Django:
data = []
data += serializers.serialize("json", conversation_deal)
data += serializers.serialize("json", deal_statuses)
dat = serializers.serialize("json", data)
return HttpResponse(dat)
I would like to save one round trip to the webservice and combine two jsons into one. What I tried to do was to serialize each object into json and add them into an array and serialize them again as json. But it throws an exception.
I also tried to put the two python objects into the array and serialize them all into json, which also failed.
How is this usually done?
Update:
Exception:
str: 'str' object has no attribute '_meta'
Update 2:
I have some more information, and it seems its somehow Django related the way it serializes the objects.
the following works perfectly fine:
deal_statuses = DealStatus.objects.all()
data = serializers.serialize("json", deal_statuses)
return HttpResponse(data)
but this fails..
conversation_deal = Conversation_Deal.objects.filter(conversation_id__in=call_id)
data = serializers.serialize("json", conversation_deal)
return HttpResponse(data)
This is how Conversation_Deal is modelled:
class Conversation_Deal(models.Model):
conversation = models.ForeignKey('Conversation')
deal = models.ForeignKey('Deal')
status = models.ForeignKey(DealStatus, null=True, blank=True)
I found something related to inherited classes documentation pointing this out why...Even though I am not inheriting, but the same process worked in my case. Hope this helps someone else. I will post this as an answer soon.
Upvotes: 0
Views: 1559
Reputation: 66320
I found the solution.
Literally since Conversation_Deal has a foreignkey to DealStatus class. It needs to know about it during serialization.
all = list(Conversation_Deal.objects.filter(conversation_id__in=call_id)) + list(DealStatus.objects.all())
return HttpResponse(simplejson.dumps(to_json), mimetype='application/json')
Hope this helps somebody else.
Upvotes: 1