Skip to content

Commit 71e1a39

Browse files
author
Carlton Gibson
committed
Initial link from Serializers to Validators
1 parent b8f396b commit 71e1a39

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

docs/api-guide/serializers.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ If your object instances correspond to Django models you'll also want to ensure
104104
instance.created = validated_data.get('created', instance.created)
105105
instance.save()
106106
return instance
107-
107+
108108
Now when deserializing data, we can call `.save()` to return an object instance, based on the validated data.
109109

110110
comment = serializer.save()
@@ -113,7 +113,7 @@ Calling `.save()` will either create a new instance, or update an existing insta
113113

114114
# .save() will create a new instance.
115115
serializer = CommentSerializer(data=data)
116-
116+
117117
# .save() will update the existing `comment` instance.
118118
serializer = CommentSerializer(comment, data=data)
119119

@@ -140,7 +140,7 @@ For example:
140140
class ContactForm(serializers.Serializer):
141141
email = serializers.EmailField()
142142
message = serializers.CharField()
143-
143+
144144
def save(self):
145145
email = self.validated_data['email']
146146
message = self.validated_data['message']
@@ -230,7 +230,7 @@ Serializer classes can also include reusable validators that are applied to the
230230
name = serializers.CharField()
231231
room_number = serializers.IntegerField(choices=[101, 102, 103, 201])
232232
date = serializers.DateField()
233-
233+
234234
class Meta:
235235
# Each room only has one event per day.
236236
validators = UniqueTogetherValidator(
@@ -448,7 +448,7 @@ To do so, open the Django shell, using `python manage.py shell`, then import the
448448
id = IntegerField(label='ID', read_only=True)
449449
name = CharField(allow_blank=True, max_length=100, required=False)
450450
owner = PrimaryKeyRelatedField(queryset=User.objects.all())
451-
451+
452452
## Specifying which fields should be included
453453

454454
If you only want a subset of the default fields to be used in a model serializer, you can do so using `fields` or `exclude` options, just as you would with a `ModelForm`.
@@ -505,6 +505,19 @@ This option should be a list or tuple of field names, and is declared as follows
505505

506506
Model fields which have `editable=False` set, and `AutoField` fields will be set to read-only by default, and do not need to be added to the `read_only_fields` option.
507507

508+
---
509+
510+
**Note**: There is a special-case where a read-only field is part of a `unique_together` constraint at the model level. Here you **must** specify the field explicitly and provide a valid default value.
511+
512+
A common example of this is a read-only relation to currently authenticated `User` which is `unique_together` with another identifier. In this case you would declare the user field like so:
513+
514+
user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault())
515+
516+
Please review the [Validators Documentation](/api-guide/validators/) for details on the [UniqueTogetherValidator](/api-guide/validators/#uniquetogethervalidator) and [CurrentUserDefault](/api-guide/validators/#currentuserdefault) classes.
517+
518+
---
519+
520+
508521
## Specifying additional keyword arguments for fields.
509522

510523
There is also a shortcut allowing you to specify arbitrary additional keyword arguments on fields, using the `extra_kwargs` option. Similarly to `read_only_fields` this means you do not need to explicitly declare the field on the serializer.
@@ -516,7 +529,7 @@ This option is a dictionary, mapping field names to a dictionary of keyword argu
516529
model = User
517530
fields = ('email', 'username', 'password')
518531
extra_kwargs = {'password': {'write_only': True}}
519-
532+
520533
def create(self, validated_data):
521534
user = User(
522535
email=validated_data['email'],
@@ -656,7 +669,7 @@ To support multiple updates you'll need to do so explicitly. When writing your m
656669
* How do you determine which instance should be updated for each item in the list of data?
657670
* How should insertions be handled? Are they invalid, or do they create new objects?
658671
* How should removals be handled? Do they imply object deletion, or removing a relationship? Should they be silently ignored, or are they invalid?
659-
* How should ordering be handled? Does changing the position of two items imply any state change or is it ignored?
672+
* How should ordering be handled? Does changing the position of two items imply any state change or is it ignored?
660673

661674
Here's an example of how you might choose to implement multiple updates:
662675

0 commit comments

Comments
 (0)