Made it possible to explicitly bypass check for nested writes

This allows for reusing create() and update() methods of ModelSerializer class when one wants to handle nested writes, but does not want to reimplement or copy the code that is already there in these methods. So, in a subclass of ModelSerializer, one can just call the parent's create() or update() method with check_for_nested_writes=False.
This commit is contained in:
Petros Moisiadis 2015-03-31 20:05:23 +03:00
parent ba951f3339
commit c267b71331

View File

@ -737,7 +737,7 @@ class ModelSerializer(Serializer):
# Default `create` and `update` behavior... # Default `create` and `update` behavior...
def create(self, validated_data): def create(self, validated_data, check_for_nested_writes=True):
""" """
We have a bit of extra checking around this in order to provide We have a bit of extra checking around this in order to provide
descriptive messages when something goes wrong, but this method is descriptive messages when something goes wrong, but this method is
@ -758,7 +758,8 @@ class ModelSerializer(Serializer):
If you want to support writable nested relationships you'll need If you want to support writable nested relationships you'll need
to write an explicit `.create()` method. to write an explicit `.create()` method.
""" """
raise_errors_on_nested_writes('create', self, validated_data) if check_for_nested_writes:
raise_errors_on_nested_writes('create', self, validated_data)
ModelClass = self.Meta.model ModelClass = self.Meta.model
@ -797,8 +798,9 @@ class ModelSerializer(Serializer):
return instance return instance
def update(self, instance, validated_data): def update(self, instance, validated_data, check_for_nested_writes=True):
raise_errors_on_nested_writes('update', self, validated_data) if check_for_nested_writes:
raise_errors_on_nested_writes('update', self, validated_data)
for attr, value in validated_data.items(): for attr, value in validated_data.items():
setattr(instance, attr, value) setattr(instance, attr, value)