0

I have a Django queryset containing model objects with some fields being Decimal objects, a format that doesn't exist in json. I know how to use Django serializers to convert this queryset to json.

However, I need to wrap the queryset in a dictionary before sending it to the front-end, like so:

{ "type": "stream1", "data": queryset }

Serializers don't work here. Error is "AttributeError: 'str' object has no attribute '_meta'". I understand why this is the case.

What I've tried (nested json objects):

I serialized the queryset and then added it to the dictionary before converting the dictionary to json. But this is inelegant since it requires the front-end to parse the dictionary first, and then parse the serialized value inside it. Makes for a poor experience.

How do I serialize the dictionary containing the Django queryset in one shot?

oktested
  • 81
  • 1
  • 7

1 Answers1

0

Didn't receive an answer to my question here but this (Django section) solved my problem, even though it was not in the context of Decimal objects, but datetime objects.

DjangoJSONEncoder ftw.

oktested
  • 81
  • 1
  • 7