I'm trying to dump django models into JSON format. I've tried to do it like this:
import json
from api.models import User
from django.http import HttpResponse
def users(request):
users = User.objects.all()
return HttpResponse(json.dumps(users), content_type="application/json")
but it throws following error:
[<User: Paul McCartney>, <User: John Lennon>, <User: George Harrison>, <User: Ringo Starr>] is not JSON serializable
I know I can iterate through all objects and create a manual list of dictionaries, but I hope there's any better way to do that. Is there?