Skip to content

Instantly share code, notes, and snippets.

@microft
Created March 19, 2024 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save microft/abe2b7945905797c68e62aabfbad6557 to your computer and use it in GitHub Desktop.
Save microft/abe2b7945905797c68e62aabfbad6557 to your computer and use it in GitHub Desktop.
serialize a Model to JSON
from .models import YourModel
from .serializers import YourModelSerializer
from rest_framework.renderers import JSONRenderer
# Assuming you have a YourModel instance
instance = YourModel.objects.get(pk=1)
# Serialize the instance using the serializer
serializer = YourModelSerializer(instance)
# Render the serialized data to JSON
json_data = JSONRenderer().render(serializer.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment