From f9f3e956c2453802c7a5a13f4540c82b3ddc1831 Mon Sep 17 00:00:00 2001 From: Udit Prabhakar Date: Wed, 22 Apr 2026 19:49:45 +0530 Subject: [PATCH 1/3] Docs: Explain object-level validation during partial updates (PATCH) #3070 --- docs/api-guide/serializers.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 109f05bf1c..6498cc711a 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -214,6 +214,26 @@ To do any other validation that requires access to multiple fields, add a method raise serializers.ValidationError("finish must occur after start") return data +!!! note + **Object-Level Validation and Partial Updates** + + When performing a **partial update** (`PATCH`), the `data` dictionary passed to the `validate()` method will **only contain the fields** provided in the request. If your validation logic depends on multiple fields, you must account for cases where some fields are missing from `data`. + + To access fields not included in the partial update, you should retrieve them from the existing object instance using `self.instance`: + + ```python + def validate(self, data): + # Check if 'status' is being updated, or use the existing value + status = data.get('status', self.instance.status) + + # Check if 'price' is being updated, or use the existing value + price = data.get('price', self.instance.price) + + if status == 'active' and price <= 0: + raise serializers.ValidationError("Active products must have a price.") + return data + ``` + #### Validators Individual fields on a serializer can include validators, by declaring them on the field instance, for example: From 6129e415fcf5a69099df8f7b7d75282a35f7d5a5 Mon Sep 17 00:00:00 2001 From: udit prabhakar Date: Wed, 22 Apr 2026 19:57:39 +0530 Subject: [PATCH 2/3] Docs: Fixed trailing whitespace issue in serializers.md --- docs/api-guide/serializers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 6498cc711a..1cc2e29e4f 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -225,7 +225,7 @@ To do any other validation that requires access to multiple fields, add a method def validate(self, data): # Check if 'status' is being updated, or use the existing value status = data.get('status', self.instance.status) - + # Check if 'price' is being updated, or use the existing value price = data.get('price', self.instance.price) From f7b60887b62fbf4de7391a71ce43910eff561176 Mon Sep 17 00:00:00 2001 From: udit prabhakar Date: Wed, 22 Apr 2026 20:07:42 +0530 Subject: [PATCH 3/3] Docs: Final linting fix for object-level validation note (blacken-docs) --- docs/api-guide/serializers.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 1cc2e29e4f..2ec98f01bf 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -223,13 +223,13 @@ To do any other validation that requires access to multiple fields, add a method ```python def validate(self, data): - # Check if 'status' is being updated, or use the existing value - status = data.get('status', self.instance.status) + # Check if "status" is being updated, or use the existing value + status = data.get("status", self.instance.status) - # Check if 'price' is being updated, or use the existing value - price = data.get('price', self.instance.price) + # Check if "price" is being updated, or use the existing value + price = data.get("price", self.instance.price) - if status == 'active' and price <= 0: + if status == "active" and price <= 0: raise serializers.ValidationError("Active products must have a price.") return data ```