From b0ca35ea5517f98e65eb6d9405a17fe991b3b0d7 Mon Sep 17 00:00:00 2001 From: liyilun Date: Mon, 8 Jul 2024 19:11:48 +0800 Subject: [PATCH] Fix One field that is required=False, allow_null=True and not provided. to_representation should not explicitly set it to None --- rest_framework/fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index cbc02e2c2..97593eeee 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -450,10 +450,10 @@ class Field: except (KeyError, AttributeError) as exc: if self.default is not empty: return self.get_default() - if self.allow_null: - return None if not self.required: raise SkipField() + if self.allow_null: + return None msg = ( 'Got {exc_type} when attempting to get a value for field ' '`{field}` on serializer `{serializer}`.\nThe serializer '