Django Rest Framework and JSONField

后端 未结 11 2013
眼角桃花
眼角桃花 2020-11-29 03:28

Given a Django model with a JSONField, what is the correct way of serializing and deserializing it using Django Rest Framework?

I\'ve already tried crating a custom

11条回答
  •  没有蜡笔的小新
    2020-11-29 04:17

    DRF gives us inbuilt field 'JSONField' for binary data, but JSON payload is verified only when you set 'binary' flag True then it convert into utf-8 and load the JSON payload, else it only treat them as string(if invalid json is sent) or json and validate both without error even though you cretaed JSONField

    class JSONSerializer(serializers.ModelSerializer):
        """
        serializer for JSON
        """
        payload = serializers.JSONField(binary=True)
    

提交回复
热议问题