diff options
| author | Tom Christie | 2012-12-12 20:44:55 +0000 |
|---|---|---|
| committer | Tom Christie | 2012-12-12 20:45:06 +0000 |
| commit | 497da7fc699b9e88c966e37bc48739865336683d (patch) | |
| tree | 57677cec1de1ab39f4561af8c647f27fe503941c /rest_framework/serializers.py | |
| parent | 628e3bf001ca71da48a6f3c7bbdf209f2e20b223 (diff) | |
| download | django-rest-framework-497da7fc699b9e88c966e37bc48739865336683d.tar.bz2 | |
Clean up field initialization. Fixes #497
Diffstat (limited to 'rest_framework/serializers.py')
| -rw-r--r-- | rest_framework/serializers.py | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5465d7b7..caa7c980 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -100,7 +100,8 @@ class BaseSerializer(Field): _options_class = SerializerOptions _dict_class = SortedDictWithMetadata # Set to unsorted dict for backwards compatibility with unsorted implementations. - def __init__(self, instance=None, data=None, files=None, context=None, partial=False, **kwargs): + def __init__(self, instance=None, data=None, files=None, + context=None, partial=False, **kwargs): super(BaseSerializer, self).__init__(**kwargs) self.opts = self._options_class(self.Meta) self.parent = None @@ -151,8 +152,6 @@ class BaseSerializer(Field): base_fields = copy.deepcopy(self.base_fields) for key, field in base_fields.items(): ret[key] = field - # Set up the field - field.initialize(parent=self, field_name=key) # Add in the default fields default_fields = self.get_default_fields() @@ -172,6 +171,10 @@ class BaseSerializer(Field): for key in self.opts.exclude: ret.pop(key, None) + # Initialize the fields + for key, field in ret.items(): + field.initialize(parent=self, field_name=key) + return ret ##### @@ -186,6 +189,13 @@ 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. |
