[Answer]-Django: Passing multiple json objects from view

1👍

You could combine them into one object and then send them, because you can send only a single dict as response:

e.g.

obj = {
   'car': serializers.serialize('json', [Car.objects.get(id=number)]).strip("[]"),
   'person': serializers.serialize('json', [Person.objects.get(name=car.owner)]).strip("[]")
}

obj_json = json.dumps(obj)

You can also use Car.objects.filter(id=number) and Person.objects.filter(name=car.owner).. Just a little optimization

0👍

all_objects = list(Restaurant.objects.all()) + list(Place.objects.all())
data = serializers.serialize('json', all_objects)

More: https://docs.djangoproject.com/en/dev/topics/serialization/

Leave a comment