Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I am confused regarding the DRF Serializer Documentation:
For serializers, it was mentioned that,
If your object instances correspond to Django models you'll also want to ensure that these methods save the object to the database. For example, if Comment was a Django model, the methods might look like this:
def create(self, validated_data):
return Comment.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.content = validated_data.get('content', instance.content)
instance.created = validated_data.get('created', instance.created)
instance.save()
return instance
From serializers.py code, it seems that .save() will update the instance in the database.
However, it was later mentioned,
Now when deserializing data, we can call .save() to return an object instance, based on the validated data.
comment = serializer.save()
Calling .save() will either create a new instance, or update an existing instance, depending on if an existing instance was passed when instantiating the serializer class:
# .save() will create a new instance.
serializer = CommentSerializer(data=data)
# .save() will update the existing `comment` instance.
serializer = CommentSerializer(comment, data=data)
It seems that calling serializer.save() in views.py will update the 'comment' instance in the database. Calling instance.save() in serializers.py will also update the 'comment' instance in the database.
My question is if there is a difference between the two methods of saving / updating an instance to the database?
If you check what serializer.save()
is doing, it will make more sense (removed some lines):
def save(self, **kwargs):
validated_data = dict(
list(self.validated_data.items()) +
list(kwargs.items())
if self.instance is not None:
self.instance = self.update(self.instance, validated_data)
assert self.instance is not None, (
'`update()` did not return an object instance.'
else:
self.instance = self.create(validated_data)
assert self.instance is not None, (
'`create()` did not return an object instance.'
return self.instance
So everytime you call Serializer's save()
, you get an instance of whatever data you are serializing.
So in this case:
# .save() will update the existing `comment` instance.
serializer = CommentSerializer(comment, data=data)
comment
here is an instance. Passing an instance in a serializer is telling the serializer that you want to update this instance. So calling save
will then call self.update
.
While:
# .save() will create a new instance.
serializer = CommentSerializer(data=data)
does not have an instance (you only have the data you are trying to serialize), so calling save
will call self.create
.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.