[Answer]-Django: Serialize to JSON a dictionary with items that can include querysets

0👍

Well, I found myself a solution. I think I can help other people with that.

I created my own encoder class to format the querysets as much as I want.

class MyEncoder(json.JSONEncoder):
    """ json.JSONEncoder extension: handle querysets """
    def default(self, obj):
        if isinstance(obj, QuerySet) or isinstance(obj, RawQuerySet):
            return serializers.serialize('python', obj, ensure_ascii=False)
        return json.JSONEncoder.default(self, obj)

then, in my view function:

data['ok'] = True
data['component_list'] = component_list
return HttpResponse(json.dumps(data, cls=MyEncoder),  content_type='application/json')
👤Ruben

1👍

Did this work?

data = {}
data['ok'] = True
data['component_list'] = component_list
data = serializers.serialize('json', data) # or json.dumps(data)
return HttpResponse(data, content_type='application/json')

0👍

Improving this by using DjangoJSONEncoder, as @tburette suggested…

class MyJSONEncoder(DjangoJSONEncoder):
    def default(self, obj):
        if isinstance(obj, QuerySet) or isinstance(obj, RawQuerySet):
            return serializers.serialize('python', obj, ensure_ascii=False, use_natural_foreign_keys=True)
        return super().default(obj)

I call this like:

from common.helpers import MyJSONEncoder

x = Part.objects.filter(description__icontains=q)[:10].only('part_code','description')
y = Part.objects.filter(description__icontains=q).count()
data = {'x': x, 'y': y} 
return JsonResponse(data, encoder=MyJSONEncoder)
👤Ruben

Leave a comment