1

If i use this

data = serializers.serialize('json', Book.objects.all())
return HttpResponse(data, mimetype='application/javascript') # Redirect after POST

Then i get json objects

but if i need to return single object

then i get error

data = serializers.serialize('json', singleObject)
return HttpResponse(data, mimetype='application/javascript') # Redirect after POST

The error says

object is not iterable

2 Answers 2

3

Read the docs on serialization

The arguments to the serialize function are the format to serialize the data to (see Serialization formats) and a QuerySet to serialize. (Actually, the second argument can be any iterator that yields Django objects, but it'll almost always be a QuerySet).

And then try like this:

data = serializers.serialize('json', [singleObject])

Also, this thread answers your question.

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

Comments

1

You can use filter instead of get to return single object.

Comments

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.