This method implements the creation of a `ListSerializer` parent class when `many=True` is used. You can customize it if you need to control which keyword arguments are passed to the parent, and which are passed to the child. Note that we're over-cautious in
(cls, *args, **kwargs)
| 133 | |
| 134 | @classmethod |
| 135 | def many_init(cls, *args, **kwargs): |
| 136 | """ |
| 137 | This method implements the creation of a `ListSerializer` parent |
| 138 | class when `many=True` is used. You can customize it if you need to |
| 139 | control which keyword arguments are passed to the parent, and |
| 140 | which are passed to the child. |
| 141 | |
| 142 | Note that we're over-cautious in passing most arguments to both parent |
| 143 | and child classes in order to try to cover the general case. If you're |
| 144 | overriding this method you'll probably want something much simpler, eg: |
| 145 | |
| 146 | @classmethod |
| 147 | def many_init(cls, *args, **kwargs): |
| 148 | kwargs['child'] = cls() |
| 149 | return CustomListSerializer(*args, **kwargs) |
| 150 | """ |
| 151 | list_kwargs = {} |
| 152 | for key in LIST_SERIALIZER_KWARGS_REMOVE: |
| 153 | value = kwargs.pop(key, None) |
| 154 | if value is not None: |
| 155 | list_kwargs[key] = value |
| 156 | list_kwargs['child'] = cls(*args, **kwargs) |
| 157 | list_kwargs.update({ |
| 158 | key: value for key, value in kwargs.items() |
| 159 | if key in LIST_SERIALIZER_KWARGS |
| 160 | }) |
| 161 | meta = getattr(cls, 'Meta', None) |
| 162 | list_serializer_class = getattr(meta, 'list_serializer_class', ListSerializer) |
| 163 | return list_serializer_class(*args, **list_kwargs) |
| 164 | |
| 165 | def to_internal_value(self, data): |
| 166 | raise NotImplementedError('`to_internal_value()` must be implemented.') |