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
tej.tan
  • 4,067
  • 6
  • 28
  • 29

2 Answers2

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.

Community
  • 1
  • 1
Davor Lucic
  • 28,970
  • 8
  • 66
  • 76
1

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

imilbaev
  • 864
  • 12
  • 19