diff options
| author | Tom Christie | 2014-09-25 11:40:32 +0100 | 
|---|---|---|
| committer | Tom Christie | 2014-09-25 11:40:32 +0100 | 
| commit | 64632da3718f501cb8174243385d38b547c2fefd (patch) | |
| tree | cb1f7eba968fe61a302639008ba6c7e32678c329 /rest_framework/fields.py | |
| parent | b22c9602fa0f717b688fdb35e4f6f42c189af3f3 (diff) | |
| download | django-rest-framework-64632da3718f501cb8174243385d38b547c2fefd.tar.bz2 | |
Clean up bind - no longer needs to be called multiple times in nested fields
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 21 | 
1 files changed, 16 insertions, 5 deletions
| diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d1aebbaf..446732c3 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -109,7 +109,8 @@ class Field(object):      def __init__(self, read_only=False, write_only=False,                   required=None, default=empty, initial=None, source=None,                   label=None, help_text=None, style=None, -                 error_messages=None, validators=[], allow_null=False): +                 error_messages=None, validators=[], allow_null=False, +                 context=None):          self._creation_counter = Field._creation_counter          Field._creation_counter += 1 @@ -135,6 +136,11 @@ class Field(object):          self.validators = validators or self.default_validators[:]          self.allow_null = allow_null +        # These are set up by `.bind()` when the field is added to a serializer. +        self.field_name = None +        self.parent = None +        self._context = {} if (context is None) else context +          # Collect default error message from self and parent classes          messages = {}          for cls in reversed(self.__class__.__mro__): @@ -157,7 +163,14 @@ class Field(object):          kwargs = copy.deepcopy(self._kwargs)          return self.__class__(*args, **kwargs) -    def bind(self, field_name, parent, root): +    @property +    def context(self): +        root = self +        while root.parent is not None: +            root = root.parent +        return root._context + +    def bind(self, field_name, parent):          """          Setup the context for the field instance.          """ @@ -174,10 +187,8 @@ class Field(object):          self.field_name = field_name          self.parent = parent -        self.root = root -        self.context = parent.context -        # `self.label` should deafult to being based on the field name. +        # `self.label` should default to being based on the field name.          if self.label is None:              self.label = field_name.replace('_', ' ').capitalize() | 
