You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add disabling of declared fields on serializer subclasses (#4764)
* Add test for disabling declared fields on child
* Check that declared base field is not in attrs
* Update meta inheritance docs to include serializer
* Test that meta fields cannot be declared as None
* Add docs example for declarative field disabling
Copy file name to clipboardExpand all lines: docs/api-guide/serializers.md
+34-10Lines changed: 34 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -578,16 +578,6 @@ Alternative representations include serializing using hyperlinks, serializing co
578
578
579
579
For full details see the [serializer relations][relations] documentation.
580
580
581
-
## Inheritance of the 'Meta' class
582
-
583
-
The inner `Meta` class on serializers is not inherited from parent classes by default. This is the same behavior as with Django's `Model` and `ModelForm` classes. If you want the `Meta` class to inherit from a parent class you must do so explicitly. For example:
584
-
585
-
class AccountSerializer(MyBaseSerializer):
586
-
class Meta(MyBaseSerializer.Meta):
587
-
model = Account
588
-
589
-
Typically we would recommend *not* using inheritance on inner Meta classes, but instead declaring all options explicitly.
590
-
591
581
## Customizing field mappings
592
582
593
583
The ModelSerializer class also exposes an API that you can override in order to alter how serializer fields are automatically determined when instantiating the serializer.
@@ -1025,6 +1015,40 @@ If any of the validation fails, then the method should raise a `serializers.Vali
1025
1015
1026
1016
The `data` argument passed to this method will normally be the value of `request.data`, so the datatype it provides will depend on the parser classes you have configured for your API.
1027
1017
1018
+
## Serializer Inheritance
1019
+
1020
+
Similar to Django forms, you can extend and reuse serializers through inheritance. This allows you to declare a common set of fields or methods on a parent class that can then be used in a number of serializers. For example,
1021
+
1022
+
class MyBaseSerializer(Serializer):
1023
+
my_field = serializers.CharField()
1024
+
1025
+
def validate_my_field(self):
1026
+
...
1027
+
1028
+
class MySerializer(MyBaseSerializer):
1029
+
...
1030
+
1031
+
Like Django's `Model` and `ModelForm` classes, the inner `Meta` class on serializers does not implicitly inherit from it's parents' inner `Meta` classes. If you want the `Meta` class to inherit from a parent class you must do so explicitly. For example:
1032
+
1033
+
class AccountSerializer(MyBaseSerializer):
1034
+
class Meta(MyBaseSerializer.Meta):
1035
+
model = Account
1036
+
1037
+
Typically we would recommend *not* using inheritance on inner Meta classes, but instead declaring all options explicitly.
1038
+
1039
+
Additionally, the following caveats apply to serializer inheritance:
1040
+
1041
+
* Normal Python name resolution rules apply. If you have multiple base classes that declare a `Meta` inner class, only the first one will be used. This means the child’s `Meta`, if it exists, otherwise the `Meta` of the first parent, etc.
1042
+
* It’s possible to declaratively remove a `Field` inherited from a parent class by setting the name to be `None` on the subclass.
1043
+
1044
+
class MyBaseSerializer(ModelSerializer):
1045
+
my_field = serializers.CharField()
1046
+
1047
+
class MySerializer(MyBaseSerializer):
1048
+
my_field = None
1049
+
1050
+
However, you can only use this technique to opt out from a field defined declaratively by a parent class; it won’t prevent the `ModelSerializer` from generating a default field. To opt-out from default fields, see [Specifying which fields to include](#specifying-which-fields-to-include).
1051
+
1028
1052
## Dynamically modifying fields
1029
1053
1030
1054
Once a serializer has been initialized, the dictionary of fields that are set on the serializer may be accessed using the `.fields` attribute. Accessing and modifying this attribute allows you to dynamically modify the serializer.
0 commit comments