Django Rest Framework and JSONField

后端 未结 11 2003
眼角桃花
眼角桃花 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:25

    If and only if you know the first-level style of your JSON content (List or Dict), you can use DRF builtin DictField or ListField.

    Ex:

    class MyModelSerializer(serializers.HyperlinkedModelSerializer):
        my_json_field = serializers.DictField()
    

    It works fine, with GET/PUT/PATCH/POST, including with nested contents.

提交回复
热议问题