About Creating JSON Strings with Django-specific Values

To make sure that UUID values, decimals, dates, and lazy objects get encoded for JSON, pass the DjangoJSONEncoder to the json.dumps() function:

1
2
3
4
import json
from django.core.serializers.json import DjangoJSONEncoder

data_json = json.dumps(data, cls=DjangoJSONEncoder)

Tips and Tricks Programming Security Django 5.x Django 4.2 Django 3.2 JSON