def default_field_constructor(self, instance, spec, loc, context,
predicate_type):
"""
A common constructor for the drf fields.
There are two cases:
* If the field is a model field, then it does not initialize a
`serializers.Field` object, but it stores all its properties in
dictionary in order to be initialized later from the serializer.
* If the field is a non-model field or its type is either `.struct`
or `.structarry`, then the corresponding `serializers.Field` is
contructed.
Moreover, this method checks if the field conforms to the model
configuations before being constructed.
"""
model, automated = self.validate_model_configuration(
instance, spec, loc, context, predicate_type)
path = (self.ADAPTER_CONF,)
instance_source = spec.pop('instance_source', None)
onmodel = spec.get('onmodel', True)
if instance_source and onmodel:
raise utils.DRFAdapterException(
'`instance_source` and `onmodel=True` are mutually'
' exclusive.', loc=loc)
field_kwargs = {k: v for k, v in spec.iteritems() if k != 'onmodel'}
field_kwargs.update(doc.doc_get(instance, path) or {})
field_kwargs.update(self._get_extra_field_kwargs(
predicate_type, instance, loc, context, automated, field_kwargs))
doc.doc_set(instance, (self.ADAPTER_CONF, 'source'), instance_source)
drf_field = self._generate_field(
instance, context.get('parent_name'), predicate_type, model,
automated and onmodel, **field_kwargs)
doc.doc_set(instance, (self.ADAPTER_CONF, 'field'), drf_field)
return instance
评论列表
文章目录