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
'IT > Django' 카테고리의 다른 글
vscode에서 dockerized 된 django 디버깅 방법 (1) | 2024.01.09 |
---|---|
Django에서 Celery가 필요한 이유? (0) | 2024.01.04 |
django request.user가 anonymous로만 나오는 이유? (0) | 2023.06.28 |
제대로된 해결 방법 - received a naive datetime while time zone support is active. (0) | 2023.06.22 |
How to create Djoser custom token strategy more detail info(jwt 정보추가) (0) | 2023.06.01 |