mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 03:50:11 +03:00
Altered the code to avoid spanning more than one relation.
This commit is contained in:
parent
214ce0777d
commit
be7183f196
|
@ -950,24 +950,28 @@ class ModelSerializer(Serializer):
|
||||||
# Update an existing instance...
|
# Update an existing instance...
|
||||||
if instance is not None:
|
if instance is not None:
|
||||||
for key, val in attrs.items():
|
for key, val in attrs.items():
|
||||||
# Follow relations if needed
|
|
||||||
dest = instance
|
|
||||||
keys = key.split('.')
|
keys = key.split('.')
|
||||||
try:
|
|
||||||
for related_instance in keys[:-1]:
|
|
||||||
dest = getattr(dest, related_instance)
|
|
||||||
|
|
||||||
except AttributeError:
|
# Work on the current instance for this attribute
|
||||||
self._errors[key] = self.error_messages['missing']
|
attr_instance = instance
|
||||||
|
|
||||||
|
# Raise an error if we span more than one relation
|
||||||
|
if len(keys) > 2:
|
||||||
|
self._errors[key] = 'Can not span more than a relation'
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# If there's a relation, mark the object to save
|
# Mark the related instance as the one to save
|
||||||
if len(keys) > 1:
|
if len(keys) == 2:
|
||||||
related_models_to_save[key] = dest
|
try:
|
||||||
|
attr_instance = getattr(instance, keys[0])
|
||||||
|
related_models_to_save[key] = attr_instance
|
||||||
|
except AttributeError:
|
||||||
|
self._errors[key] = self.error_messages['missing']
|
||||||
|
continue
|
||||||
|
|
||||||
# Assign the value
|
# Assign the value
|
||||||
try:
|
try:
|
||||||
setattr(dest, keys[-1], val)
|
setattr(attr_instance, keys[-1], val)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self._errors[key] = self.error_messages['required']
|
self._errors[key] = self.error_messages['required']
|
||||||
|
|
||||||
|
@ -976,7 +980,6 @@ class ModelSerializer(Serializer):
|
||||||
for key, value in ((k, v) for k, v in attrs.items() if '.' in k):
|
for key, value in ((k, v) for k, v in attrs.items() if '.' in k):
|
||||||
self._errors[key] = 'You can not set dotted sources during creation.'
|
self._errors[key] = 'You can not set dotted sources during creation.'
|
||||||
del attrs[key]
|
del attrs[key]
|
||||||
print value
|
|
||||||
instance = self.opts.model(**attrs)
|
instance = self.opts.model(**attrs)
|
||||||
|
|
||||||
# Any relations that cannot be set until we've
|
# Any relations that cannot be set until we've
|
||||||
|
|
Loading…
Reference in New Issue
Block a user