aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/serializers.py
diff options
context:
space:
mode:
authorMichal Dvorak (cen38289)2012-12-21 10:53:23 +0100
committerMichal Dvorak (cen38289)2012-12-21 10:53:23 +0100
commit5ba2437f2dcb4eb7f9677ff9e393c27af38b071f (patch)
treedf19512bc58ae3180813c9d479267b7a617e9b8e /rest_framework/serializers.py
parent8ac77eaae8d6ad01ec8f6de18134c4aa1961d4dd (diff)
parent79aea2f0d082f17e7bb75cc32bd71b5f04836d43 (diff)
downloaddjango-rest-framework-5ba2437f2dcb4eb7f9677ff9e393c27af38b071f.tar.bz2
Merge remote-tracking branch 'tom/master'
Conflicts: rest_framework/tests/serializer.py
Diffstat (limited to 'rest_framework/serializers.py')
-rw-r--r--rest_framework/serializers.py34
1 files changed, 16 insertions, 18 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
index ed6835fa..e9bc25e4 100644
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -160,6 +160,9 @@ class BaseSerializer(Field):
for key in self.opts.exclude:
ret.pop(key, None)
+ for key, field in ret.items():
+ field.initialize(parent=self, field_name=key)
+
return ret
#####
@@ -174,13 +177,6 @@ class BaseSerializer(Field):
if parent.opts.depth:
self.opts.depth = parent.opts.depth - 1
- # We need to call initialize here to ensure any nested
- # serializers that will have already called initialize on their
- # descendants get updated with *their* parent.
- # We could be a bit more smart about this, but it'll do for now.
- for key, field in self.fields.items():
- field.initialize(parent=self, field_name=key)
-
#####
# Methods to convert or revert from objects <--> primitive representations.
@@ -507,25 +503,27 @@ class ModelSerializer(Serializer):
if instance is not None:
for key, val in attrs.items():
setattr(instance, key, val)
- return instance
- # Reverse relations
- for (obj, model) in self.opts.model._meta.get_all_related_m2m_objects_with_model():
- field_name = obj.field.related_query_name()
- if field_name in attrs:
- self.m2m_data[field_name] = attrs.pop(field_name)
+ else:
+ # Reverse relations
+ for (obj, model) in self.opts.model._meta.get_all_related_m2m_objects_with_model():
+ field_name = obj.field.related_query_name()
+ if field_name in attrs:
+ self.m2m_data[field_name] = attrs.pop(field_name)
+
+ # Forward relations
+ for field in self.opts.model._meta.many_to_many:
+ if field.name in attrs:
+ self.m2m_data[field.name] = attrs.pop(field.name)
- # Forward relations
- for field in self.opts.model._meta.many_to_many:
- if field.name in attrs:
- self.m2m_data[field.name] = attrs.pop(field.name)
+ instance = self.opts.model(**attrs)
- instance = self.opts.model(**attrs)
try:
instance.full_clean(exclude=self.get_validation_exclusions())
except ValidationError, err:
self._errors = err.message_dict
return None
+
return instance
def save(self, save_m2m=True):