Skip to content

Commit 05c8c7b

Browse files
committed
documentation for migration path
1 parent d05c9d8 commit 05c8c7b

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ py.test
231231
New-Style Serializers
232232
================
233233

234-
In 2020, an enhanced set of mixins were added that permit fine-grained control of nested
234+
In 2021, an enhanced set of mixins were added that permit fine-grained control of nested
235235
Serializer behavior using a `match_on` argument. New-style serializers delegate control
236236
of the Create/Update behavior to the nested Serializer. The parent Serializer need only
237237
resolve nested serializers in the right order; this is handled by the `RelatedSaveMixin`.
@@ -253,6 +253,33 @@ The new-style Serializers may be used as top-level Serializers to provide get-or
253253
behaviors to DRF endpoints. Examples of use can be found in
254254
`test_nested_serializer_mixins.py`.
255255

256+
Migration
257+
---------
258+
259+
To convert an existing serializer to the new style serializers, the following procedure
260+
is recommended:
261+
262+
1. Convert nested serializers by replacing `serializers.ModelSerializer` with
263+
`UpdateOrCreateNestedSerializerMixin, serializers.ModelSerializer` which preserves
264+
backwards-compatible behavior.
265+
1. Convert parent serializer by replacing `WritableNestedModelSerializer` with
266+
`RelatedSaveMixin, serializers.ModelSerializer`.
267+
1. Verify that your test cases still pass.
268+
1. Modify serializers (and test case) based on desired new-style behavior e.g. by
269+
adding an explicit `match_on` or switching to `GetOrCreateNestedSerializerMixin`
270+
behavior.
271+
272+
All test cases were duplicated for new-style serializers so you can see examples of
273+
converted serializers in `tests/serializers.py`. For example `TeamSerializer` and
274+
`UserSerializer` become `NewTeamSerializer` and `NewUserSerializer`. Examples of
275+
`DEFAULT_MATCH_ON` can be found in `tests/serializers.py`. One example of an explicit
276+
specified `match_on` is present, but non-default `match_on` values are not found in
277+
`tests` because they were not required to produce existing behaviors.
278+
279+
NOTE: While `RelatedSaveMixin` is the backwards-compatible mixin for the top-level
280+
class, it is also possible to use other mixins to get complex matching behavior without
281+
modifying the view.
282+
256283
Known problems with solutions
257284
=============================
258285

0 commit comments

Comments
 (0)