1👍
✅
The json.dumps
function is meant to convert certain Python objects to a a JSON string. But you are already serializing your model instances (via QuerySets) to JSON strings, and json.dumps
is trying to convert these strings into JSON all over again–it only knows that you’ve passed it str
objects, not that these str
objects represents JSON.
The JSON encoder used by json.dumps
only knows how to convert a handful of built-in types:
So what you need to do is convert your model instances to one of these types. The easiest solution would be to use django.forms.models.model_to_dict
on each element of your QuerySets, like so:
from django.forms.models import model_to_dict
response = json.dumps({
'candidate': [model_to_dict(x) for x in obj],
'CandidateEducationProfile': [model_to_dict(x) for x in ce],
...
)
Source:stackexchange.com