From 795e149c0285b376f57538920d0754da2e9320f5 Mon Sep 17 00:00:00 2001 From: MoeSalah1999 Date: Fri, 16 Jan 2026 19:57:21 +0300 Subject: [PATCH 1/5] docs: improve docstring for get_serializer to clarify behavior --- rest_framework/serializers.py | 36 +++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index ca60810df1..5b046f71c9 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -312,6 +312,18 @@ def __new__(cls, name, bases, attrs): def as_serializer_error(exc): + """ + coerce validation exceptions into a standardized serializer error format. + + this function normalizes both Django's 'ValidationError' and REST + framework's 'ValidationError' into a dictionary structure compatible + with serializer '.errors', ensuring all values are represented as + lists of error details. + + The returned structure conforms to the serializer error contract: + - Field-specific errors are returned as '{field-name: [errors]}' + - Non-field errors are returned under the 'NON_FIELD_ERRORS_KEY' + """ assert isinstance(exc, (ValidationError, DjangoValidationError)) if isinstance(exc, DjangoValidationError): @@ -815,22 +827,22 @@ def errors(self): def raise_errors_on_nested_writes(method_name, serializer, validated_data): """ - Give explicit errors when users attempt to pass writable nested data. - - If we don't do this explicitly they'd get a less helpful error when - calling `.save()` on the serializer. + Enforce explicit handling of writable nested and dotted-source fields. - We don't *automatically* support these sorts of nested writes because - there are too many ambiguities to define a default behavior. + This helper raises clear and actionable errors when a serializer attempts + to perform writable nested updates or creates using the default + `ModelSerializer` behavior. - Eg. Suppose we have a `UserSerializer` with a nested profile. How should - we handle the case of an update, where the `profile` relationship does - not exist? Any of the following might be valid: + Writable nested relationships and dotted-source fields are intentionally + unsupported by default due to ambiguous persistence semantics. Developers + must either: + - Override the `.create()` / `.update()` methods explicitly, or + - Mark nested serializers as `read_only=True` - * Raise an application error. - * Silently ignore the nested part of the update. - * Automatically create a profile instance. + This check is invoked internally by default `ModelSerializer.create()` + and `ModelSerializer.update()` implementations. """ + ModelClass = serializer.Meta.model model_field_info = model_meta.get_field_info(ModelClass) From f4b6dd0849b446cec1ce19a0a1f238688bf5c81f Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Sat, 24 Jan 2026 14:39:33 +0000 Subject: [PATCH 2/5] Tweak formatting --- rest_framework/serializers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5b046f71c9..e1708dea66 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -313,11 +313,11 @@ def __new__(cls, name, bases, attrs): def as_serializer_error(exc): """ - coerce validation exceptions into a standardized serializer error format. + Coerce validation exceptions into a standardized serialized error format. - this function normalizes both Django's 'ValidationError' and REST - framework's 'ValidationError' into a dictionary structure compatible - with serializer '.errors', ensuring all values are represented as + This function normalizes both Django's `ValidationError` and REST + framework's `ValidationError` into a dictionary structure compatible + with serializer `.errors`, ensuring all values are represented as lists of error details. The returned structure conforms to the serializer error contract: From 765b0904aaaa1491475bc16ddd377cd49650c5f8 Mon Sep 17 00:00:00 2001 From: MoeSalah1999 <161456250+MoeSalah1999@users.noreply.github.com> Date: Sat, 24 Jan 2026 19:43:43 +0300 Subject: [PATCH 3/5] Update serializers.py Re-introduced old example mentioned --- rest_framework/serializers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index e1708dea66..5f9e957d7d 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -841,6 +841,14 @@ def raise_errors_on_nested_writes(method_name, serializer, validated_data): This check is invoked internally by default `ModelSerializer.create()` and `ModelSerializer.update()` implementations. + + Eg. Suppose we have a `UserSerializer` with a nested profile. How should + we handle the case of an update, where the `profile` relationship does + not exist? Any of the following might be valid: + + * Raise an application error. + * Silently ignore the nested part of the update. + * Automatically create a profile instance. """ ModelClass = serializer.Meta.model From d8a7f9a8c2b764a4a473bf6bfe2d812f4f03a6f0 Mon Sep 17 00:00:00 2001 From: MoeSalah1999 Date: Sat, 24 Jan 2026 20:00:34 +0300 Subject: [PATCH 4/5] Removed whitespace --- rest_framework/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5f9e957d7d..7ca3b993f5 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -842,7 +842,7 @@ def raise_errors_on_nested_writes(method_name, serializer, validated_data): This check is invoked internally by default `ModelSerializer.create()` and `ModelSerializer.update()` implementations. - Eg. Suppose we have a `UserSerializer` with a nested profile. How should + Eg. Suppose we have a `UserSerializer` with a nested profile. How should we handle the case of an update, where the `profile` relationship does not exist? Any of the following might be valid: From c4839ac7055960156477634767079b12079552f4 Mon Sep 17 00:00:00 2001 From: MoeSalah1999 Date: Sat, 24 Jan 2026 20:01:45 +0300 Subject: [PATCH 5/5] Removed blank line containing white space --- rest_framework/serializers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 7ca3b993f5..5f34b00194 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -845,7 +845,6 @@ def raise_errors_on_nested_writes(method_name, serializer, validated_data): Eg. Suppose we have a `UserSerializer` with a nested profile. How should we handle the case of an update, where the `profile` relationship does not exist? Any of the following might be valid: - * Raise an application error. * Silently ignore the nested part of the update. * Automatically create a profile instance.