How can you check on save if the object was changed by the user? I.e. if any differences from the original database object were introduced. Before it was possible with pre_save()
(See object changes in post_save in django rest framework), but now that was replaced with perform_update
, which no longer holds both objects (original and modified) for comparison.
In Django REST Framework 3, pre_save
was replaced with perform_update
, which only takes the serializer as an argument (instead of the object itself).
You can access the validated data that was passed into the request using the .validated_data
attribute on the serializer. This is the recommended replacement for .object
, and should allow you to determine what the differences are.
def perform_update(self, serializer):
original_object = self.get_object() # or (the private attribute) serializer.instance
changes = serializer.validated_data
serializer.save(attr=changed_value) # pass arguments into `save` to override changes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With