Django REST framework flat, read-write serializer

后端 未结 3 1148
一整个雨季
一整个雨季 2020-12-25 08:54

In Django REST framework, what is involved in creating a flat, read-write serializer representation? The docs refer to a \'flat representation\' (end of the section http://d

3条回答
  •  清酒与你
    2020-12-25 09:20

    First: better handling of nested writes is on it's way.

    Second: The Serializer Relations docs say of both PrimaryKeyRelatedField and SlugRelatedField that "By default this field is read-write..." — so if your email field was unique (is it?) it might be you could use the SlugRelatedField and it would just work — I've not tried this yet (however).

    Third: Instead I've used a plain Field subclass that uses the source="*" technique to accept the whole object. From there I manually pull the related field in to_native and return that — this is read-only. In order to write I've checked request.DATA in post_save and updated the related object there — This isn't automatic but it works.

    So, Fourth: Looking at what you've already got, my approach (above) amounts to marking your email field as read-only and then implementing post_save to check for an email value and perform the update accordingly.

提交回复
热议问题