Created
March 19, 2024 15:08
-
-
Save microft/abe2b7945905797c68e62aabfbad6557 to your computer and use it in GitHub Desktop.
serialize a Model to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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