1

I know how to serialize a single object and then convert it into a json. But I want serialize multiple objects of model (using the serializer) and build a response that contains a json object array?

Is there a direct way of doing it? Or is there a way to make a json array with multiple json objects?

1 Answer 1

1

Here is your answer..

from django.core import serializers
serializers.serialize('json', YourModel.objects.filter(attr='foo'), fields=('field1', 'field2'))

or if you wish you can bind your serialized objects into a variable like

    serialized_data = serializers.serialize('json', YourModel.objects.filter(attr='foo'), fields=('field1', 'field2'))

then do what you want.

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

1 Comment

I'm not retrieving my objects as a query set. They're in a list. How do I go about it now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.