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.
| 112 | |
| 113 | |
| 114 | class 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 | |
| 150 | class BindingDict(MutableMapping): |
no outgoing calls
no test coverage detected