MCPcopy Create free account
hub / github.com/encode/django-rest-framework / NestedBoundField

Class NestedBoundField

rest_framework/utils/serializer_helpers.py:114–147  ·  view source on GitHub ↗

This `BoundField` additionally implements __iter__ and __getitem__ in order to support nested bound fields. This class is the type of `BoundField` that is used for serializer fields.

Source from the content-addressed store, hash-verified

112
113
114class NestedBoundField(BoundField):
115 """
116 This `BoundField` additionally implements __iter__ and __getitem__
117 in order to support nested bound fields. This class is the type of
118 `BoundField` that is used for serializer fields.
119 """
120
121 def __init__(self, field, value, errors, prefix=''):
122 if value is None or value == '' or not isinstance(value, Mapping):
123 value = {}
124 super().__init__(field, value, errors, prefix)
125
126 def __iter__(self):
127 for field in self.fields.values():
128 yield self[field.field_name]
129
130 def __getitem__(self, key):
131 field = self.fields[key]
132 value = self.value.get(key) if self.value else None
133 error = self.errors.get(key) if isinstance(self.errors, dict) else None
134 if hasattr(field, 'fields'):
135 return NestedBoundField(field, value, error, prefix=self.name + '.')
136 elif getattr(field, '_is_jsonfield', False):
137 return JSONBoundField(field, value, error, prefix=self.name + '.')
138 return BoundField(field, value, error, prefix=self.name + '.')
139
140 def as_form_field(self):
141 values = {}
142 for key, value in self.value.items():
143 if isinstance(value, (list, dict)):
144 values[key] = value
145 else:
146 values[key] = '' if (value is None or value is False) else force_str(value)
147 return self.__class__(self._field, values, self.errors, self._prefix)
148
149
150class BindingDict(MutableMapping):

Callers 2

__getitem__Method · 0.90
__getitem__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected