IT/Django
Django core serialize with FK.
bepuri
2024. 1. 8. 16:46
728x90
https://docs.djangoproject.com/ko/4.2/topics/serialization/#serialization-of-natural-keys
Look and add the code which you want to represent for FK.
For serialize, Add 'natural_key' function.
For deserialize, Add CustomModelManager with 'override get_by_natural_key" function
PS. Don't make your code messy.
Just use drf if it's possible.
#This is for serialized and json.
payload = CommentSerializer(instance).data
json_payload = JSONRenderer().render(payload)
#deserialize and get your instance
stream = io.BytesIO(payload)
data = JSONParser().parse(stream)
instance = Comment.objects.get(id=data.get("id"))
All I can acheive what I want.
Ref.https://www.django-rest-framework.org/api-guide/serializers/#serializing-objects
728x90