0

View:

def some_view(request,number):
    car=Car.objects.get(id=number)
    person=Person.objects.get(name=car.owner)
    car_json=serializers.serialize("json",[car])
    car_json = car_json.strip("[]")
    person_json=serializers.serialize("json",[person])
    person_json = person_json.strip("[]")
    return HttpResponse(car_json, mimetype="application/json")

I am able to pass single json object over to the template. But how do I need to pass both car_json and person_json?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, this did not work for me. Don't we need to put car_json and person_json in the dict?
Ohh,, yeah,, You should try that,, just replace above objects with car_jason and person_json
Being a dict object, serializer on obj is causing issues.
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/

1 Comment

in my case, car and person are single list objects

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.