serializers.py 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. """
  2. Serializers and ModelSerializers are similar to Forms and ModelForms.
  3. Unlike forms, they are not constrained to dealing with HTML output, and
  4. form encoded input.
  5. Serialization in REST framework is a two-phase process:
  6. 1. Serializers marshal between complex types like model instances, and
  7. python primitives.
  8. 2. The process of marshalling between python primitives and request and
  9. response content is handled by parsers and renderers.
  10. """
  11. import copy
  12. import inspect
  13. import traceback
  14. from collections import OrderedDict, defaultdict
  15. from collections.abc import Mapping
  16. from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
  17. from django.core.exceptions import ValidationError as DjangoValidationError
  18. from django.db import models
  19. from django.db.models.fields import Field as DjangoModelField
  20. from django.utils import timezone
  21. from django.utils.functional import cached_property
  22. from django.utils.translation import gettext_lazy as _
  23. from rest_framework.compat import postgres_fields
  24. from rest_framework.exceptions import ErrorDetail, ValidationError
  25. from rest_framework.fields import get_error_detail, set_value
  26. from rest_framework.settings import api_settings
  27. from rest_framework.utils import html, model_meta, representation
  28. from rest_framework.utils.field_mapping import (
  29. ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,
  30. get_relation_kwargs, get_url_kwargs
  31. )
  32. from rest_framework.utils.serializer_helpers import (
  33. BindingDict, BoundField, JSONBoundField, NestedBoundField, ReturnDict,
  34. ReturnList
  35. )
  36. from rest_framework.validators import (
  37. UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator,
  38. UniqueTogetherValidator
  39. )
  40. # Note: We do the following so that users of the framework can use this style:
  41. #
  42. # example_field = serializers.CharField(...)
  43. #
  44. # This helps keep the separation between model fields, form fields, and
  45. # serializer fields more explicit.
  46. from rest_framework.fields import ( # NOQA # isort:skip
  47. BooleanField, CharField, ChoiceField, DateField, DateTimeField, DecimalField,
  48. DictField, DurationField, EmailField, Field, FileField, FilePathField, FloatField,
  49. HiddenField, HStoreField, IPAddressField, ImageField, IntegerField, JSONField,
  50. ListField, ModelField, MultipleChoiceField, NullBooleanField, ReadOnlyField,
  51. RegexField, SerializerMethodField, SlugField, TimeField, URLField, UUIDField,
  52. )
  53. from rest_framework.relations import ( # NOQA # isort:skip
  54. HyperlinkedIdentityField, HyperlinkedRelatedField, ManyRelatedField,
  55. PrimaryKeyRelatedField, RelatedField, SlugRelatedField, StringRelatedField,
  56. )
  57. # Non-field imports, but public API
  58. from rest_framework.fields import ( # NOQA # isort:skip
  59. CreateOnlyDefault, CurrentUserDefault, SkipField, empty
  60. )
  61. from rest_framework.relations import Hyperlink, PKOnlyObject # NOQA # isort:skip
  62. # We assume that 'validators' are intended for the child serializer,
  63. # rather than the parent serializer.
  64. LIST_SERIALIZER_KWARGS = (
  65. 'read_only', 'write_only', 'required', 'default', 'initial', 'source',
  66. 'label', 'help_text', 'style', 'error_messages', 'allow_empty',
  67. 'instance', 'data', 'partial', 'context', 'allow_null',
  68. 'max_length', 'min_length'
  69. )
  70. ALL_FIELDS = '__all__'
  71. # BaseSerializer
  72. # --------------
  73. class BaseSerializer(Field):
  74. """
  75. The BaseSerializer class provides a minimal class which may be used
  76. for writing custom serializer implementations.
  77. Note that we strongly restrict the ordering of operations/properties
  78. that may be used on the serializer in order to enforce correct usage.
  79. In particular, if a `data=` argument is passed then:
  80. .is_valid() - Available.
  81. .initial_data - Available.
  82. .validated_data - Only available after calling `is_valid()`
  83. .errors - Only available after calling `is_valid()`
  84. .data - Only available after calling `is_valid()`
  85. If a `data=` argument is not passed then:
  86. .is_valid() - Not available.
  87. .initial_data - Not available.
  88. .validated_data - Not available.
  89. .errors - Not available.
  90. .data - Available.
  91. """
  92. def __init__(self, instance=None, data=empty, **kwargs):
  93. self.instance = instance
  94. if data is not empty:
  95. self.initial_data = data
  96. self.partial = kwargs.pop('partial', False)
  97. self._context = kwargs.pop('context', {})
  98. kwargs.pop('many', None)
  99. super().__init__(**kwargs)
  100. def __new__(cls, *args, **kwargs):
  101. # We override this method in order to automatically create
  102. # `ListSerializer` classes instead when `many=True` is set.
  103. if kwargs.pop('many', False):
  104. return cls.many_init(*args, **kwargs)
  105. return super().__new__(cls, *args, **kwargs)
  106. # Allow type checkers to make serializers generic.
  107. def __class_getitem__(cls, *args, **kwargs):
  108. return cls
  109. @classmethod
  110. def many_init(cls, *args, **kwargs):
  111. """
  112. This method implements the creation of a `ListSerializer` parent
  113. class when `many=True` is used. You can customize it if you need to
  114. control which keyword arguments are passed to the parent, and
  115. which are passed to the child.
  116. Note that we're over-cautious in passing most arguments to both parent
  117. and child classes in order to try to cover the general case. If you're
  118. overriding this method you'll probably want something much simpler, eg:
  119. @classmethod
  120. def many_init(cls, *args, **kwargs):
  121. kwargs['child'] = cls()
  122. return CustomListSerializer(*args, **kwargs)
  123. """
  124. allow_empty = kwargs.pop('allow_empty', None)
  125. max_length = kwargs.pop('max_length', None)
  126. min_length = kwargs.pop('min_length', None)
  127. child_serializer = cls(*args, **kwargs)
  128. list_kwargs = {
  129. 'child': child_serializer,
  130. }
  131. if allow_empty is not None:
  132. list_kwargs['allow_empty'] = allow_empty
  133. if max_length is not None:
  134. list_kwargs['max_length'] = max_length
  135. if min_length is not None:
  136. list_kwargs['min_length'] = min_length
  137. list_kwargs.update({
  138. key: value for key, value in kwargs.items()
  139. if key in LIST_SERIALIZER_KWARGS
  140. })
  141. meta = getattr(cls, 'Meta', None)
  142. list_serializer_class = getattr(meta, 'list_serializer_class', ListSerializer)
  143. return list_serializer_class(*args, **list_kwargs)
  144. def to_internal_value(self, data):
  145. raise NotImplementedError('`to_internal_value()` must be implemented.')
  146. def to_representation(self, instance):
  147. raise NotImplementedError('`to_representation()` must be implemented.')
  148. def update(self, instance, validated_data):
  149. raise NotImplementedError('`update()` must be implemented.')
  150. def create(self, validated_data):
  151. raise NotImplementedError('`create()` must be implemented.')
  152. def save(self, **kwargs):
  153. assert hasattr(self, '_errors'), (
  154. 'You must call `.is_valid()` before calling `.save()`.'
  155. )
  156. assert not self.errors, (
  157. 'You cannot call `.save()` on a serializer with invalid data.'
  158. )
  159. # Guard against incorrect use of `serializer.save(commit=False)`
  160. assert 'commit' not in kwargs, (
  161. "'commit' is not a valid keyword argument to the 'save()' method. "
  162. "If you need to access data before committing to the database then "
  163. "inspect 'serializer.validated_data' instead. "
  164. "You can also pass additional keyword arguments to 'save()' if you "
  165. "need to set extra attributes on the saved model instance. "
  166. "For example: 'serializer.save(owner=request.user)'.'"
  167. )
  168. assert not hasattr(self, '_data'), (
  169. "You cannot call `.save()` after accessing `serializer.data`."
  170. "If you need to access data before committing to the database then "
  171. "inspect 'serializer.validated_data' instead. "
  172. )
  173. validated_data = {**self.validated_data, **kwargs}
  174. if self.instance is not None:
  175. self.instance = self.update(self.instance, validated_data)
  176. assert self.instance is not None, (
  177. '`update()` did not return an object instance.'
  178. )
  179. else:
  180. self.instance = self.create(validated_data)
  181. assert self.instance is not None, (
  182. '`create()` did not return an object instance.'
  183. )
  184. return self.instance
  185. def is_valid(self, raise_exception=False):
  186. assert hasattr(self, 'initial_data'), (
  187. 'Cannot call `.is_valid()` as no `data=` keyword argument was '
  188. 'passed when instantiating the serializer instance.'
  189. )
  190. if not hasattr(self, '_validated_data'):
  191. try:
  192. self._validated_data = self.run_validation(self.initial_data)
  193. except ValidationError as exc:
  194. self._validated_data = {}
  195. self._errors = exc.detail
  196. else:
  197. self._errors = {}
  198. if self._errors and raise_exception:
  199. raise ValidationError(self.errors)
  200. return not bool(self._errors)
  201. @property
  202. def data(self):
  203. if hasattr(self, 'initial_data') and not hasattr(self, '_validated_data'):
  204. msg = (
  205. 'When a serializer is passed a `data` keyword argument you '
  206. 'must call `.is_valid()` before attempting to access the '
  207. 'serialized `.data` representation.\n'
  208. 'You should either call `.is_valid()` first, '
  209. 'or access `.initial_data` instead.'
  210. )
  211. raise AssertionError(msg)
  212. if not hasattr(self, '_data'):
  213. if self.instance is not None and not getattr(self, '_errors', None):
  214. self._data = self.to_representation(self.instance)
  215. elif hasattr(self, '_validated_data') and not getattr(self, '_errors', None):
  216. self._data = self.to_representation(self.validated_data)
  217. else:
  218. self._data = self.get_initial()
  219. return self._data
  220. @property
  221. def errors(self):
  222. if not hasattr(self, '_errors'):
  223. msg = 'You must call `.is_valid()` before accessing `.errors`.'
  224. raise AssertionError(msg)
  225. return self._errors
  226. @property
  227. def validated_data(self):
  228. if not hasattr(self, '_validated_data'):
  229. msg = 'You must call `.is_valid()` before accessing `.validated_data`.'
  230. raise AssertionError(msg)
  231. return self._validated_data
  232. # Serializer & ListSerializer classes
  233. # -----------------------------------
  234. class SerializerMetaclass(type):
  235. """
  236. This metaclass sets a dictionary named `_declared_fields` on the class.
  237. Any instances of `Field` included as attributes on either the class
  238. or on any of its superclasses will be include in the
  239. `_declared_fields` dictionary.
  240. """
  241. @classmethod
  242. def _get_declared_fields(cls, bases, attrs):
  243. fields = [(field_name, attrs.pop(field_name))
  244. for field_name, obj in list(attrs.items())
  245. if isinstance(obj, Field)]
  246. fields.sort(key=lambda x: x[1]._creation_counter)
  247. # Ensures a base class field doesn't override cls attrs, and maintains
  248. # field precedence when inheriting multiple parents. e.g. if there is a
  249. # class C(A, B), and A and B both define 'field', use 'field' from A.
  250. known = set(attrs)
  251. def visit(name):
  252. known.add(name)
  253. return name
  254. base_fields = [
  255. (visit(name), f)
  256. for base in bases if hasattr(base, '_declared_fields')
  257. for name, f in base._declared_fields.items() if name not in known
  258. ]
  259. return OrderedDict(base_fields + fields)
  260. def __new__(cls, name, bases, attrs):
  261. attrs['_declared_fields'] = cls._get_declared_fields(bases, attrs)
  262. return super().__new__(cls, name, bases, attrs)
  263. def as_serializer_error(exc):
  264. assert isinstance(exc, (ValidationError, DjangoValidationError))
  265. if isinstance(exc, DjangoValidationError):
  266. detail = get_error_detail(exc)
  267. else:
  268. detail = exc.detail
  269. if isinstance(detail, Mapping):
  270. # If errors may be a dict we use the standard {key: list of values}.
  271. # Here we ensure that all the values are *lists* of errors.
  272. return {
  273. key: value if isinstance(value, (list, Mapping)) else [value]
  274. for key, value in detail.items()
  275. }
  276. elif isinstance(detail, list):
  277. # Errors raised as a list are non-field errors.
  278. return {
  279. api_settings.NON_FIELD_ERRORS_KEY: detail
  280. }
  281. # Errors raised as a string are non-field errors.
  282. return {
  283. api_settings.NON_FIELD_ERRORS_KEY: [detail]
  284. }
  285. class Serializer(BaseSerializer, metaclass=SerializerMetaclass):
  286. default_error_messages = {
  287. 'invalid': _('Invalid data. Expected a dictionary, but got {datatype}.')
  288. }
  289. @cached_property
  290. def fields(self):
  291. """
  292. A dictionary of {field_name: field_instance}.
  293. """
  294. # `fields` is evaluated lazily. We do this to ensure that we don't
  295. # have issues importing modules that use ModelSerializers as fields,
  296. # even if Django's app-loading stage has not yet run.
  297. fields = BindingDict(self)
  298. for key, value in self.get_fields().items():
  299. fields[key] = value
  300. return fields
  301. @property
  302. def _writable_fields(self):
  303. for field in self.fields.values():
  304. if not field.read_only:
  305. yield field
  306. @property
  307. def _readable_fields(self):
  308. for field in self.fields.values():
  309. if not field.write_only:
  310. yield field
  311. def get_fields(self):
  312. """
  313. Returns a dictionary of {field_name: field_instance}.
  314. """
  315. # Every new serializer is created with a clone of the field instances.
  316. # This allows users to dynamically modify the fields on a serializer
  317. # instance without affecting every other serializer instance.
  318. return copy.deepcopy(self._declared_fields)
  319. def get_validators(self):
  320. """
  321. Returns a list of validator callables.
  322. """
  323. # Used by the lazily-evaluated `validators` property.
  324. meta = getattr(self, 'Meta', None)
  325. validators = getattr(meta, 'validators', None)
  326. return list(validators) if validators else []
  327. def get_initial(self):
  328. if hasattr(self, 'initial_data'):
  329. # initial_data may not be a valid type
  330. if not isinstance(self.initial_data, Mapping):
  331. return OrderedDict()
  332. return OrderedDict([
  333. (field_name, field.get_value(self.initial_data))
  334. for field_name, field in self.fields.items()
  335. if (field.get_value(self.initial_data) is not empty) and
  336. not field.read_only
  337. ])
  338. return OrderedDict([
  339. (field.field_name, field.get_initial())
  340. for field in self.fields.values()
  341. if not field.read_only
  342. ])
  343. def get_value(self, dictionary):
  344. # We override the default field access in order to support
  345. # nested HTML forms.
  346. if html.is_html_input(dictionary):
  347. return html.parse_html_dict(dictionary, prefix=self.field_name) or empty
  348. return dictionary.get(self.field_name, empty)
  349. def run_validation(self, data=empty):
  350. """
  351. We override the default `run_validation`, because the validation
  352. performed by validators and the `.validate()` method should
  353. be coerced into an error dictionary with a 'non_fields_error' key.
  354. """
  355. (is_empty_value, data) = self.validate_empty_values(data)
  356. if is_empty_value:
  357. return data
  358. value = self.to_internal_value(data)
  359. try:
  360. self.run_validators(value)
  361. value = self.validate(value)
  362. assert value is not None, '.validate() should return the validated data'
  363. except (ValidationError, DjangoValidationError) as exc:
  364. raise ValidationError(detail=as_serializer_error(exc))
  365. return value
  366. def _read_only_defaults(self):
  367. fields = [
  368. field for field in self.fields.values()
  369. if (field.read_only) and (field.default != empty) and (field.source != '*') and ('.' not in field.source)
  370. ]
  371. defaults = OrderedDict()
  372. for field in fields:
  373. try:
  374. default = field.get_default()
  375. except SkipField:
  376. continue
  377. defaults[field.source] = default
  378. return defaults
  379. def run_validators(self, value):
  380. """
  381. Add read_only fields with defaults to value before running validators.
  382. """
  383. if isinstance(value, dict):
  384. to_validate = self._read_only_defaults()
  385. to_validate.update(value)
  386. else:
  387. to_validate = value
  388. super().run_validators(to_validate)
  389. def to_internal_value(self, data):
  390. """
  391. Dict of native values <- Dict of primitive datatypes.
  392. """
  393. if not isinstance(data, Mapping):
  394. message = self.error_messages['invalid'].format(
  395. datatype=type(data).__name__
  396. )
  397. raise ValidationError({
  398. api_settings.NON_FIELD_ERRORS_KEY: [message]
  399. }, code='invalid')
  400. ret = OrderedDict()
  401. errors = OrderedDict()
  402. fields = self._writable_fields
  403. for field in fields:
  404. validate_method = getattr(self, 'validate_' + field.field_name, None)
  405. primitive_value = field.get_value(data)
  406. try:
  407. validated_value = field.run_validation(primitive_value)
  408. if validate_method is not None:
  409. validated_value = validate_method(validated_value)
  410. except ValidationError as exc:
  411. errors[field.field_name] = exc.detail
  412. except DjangoValidationError as exc:
  413. errors[field.field_name] = get_error_detail(exc)
  414. except SkipField:
  415. pass
  416. else:
  417. set_value(ret, field.source_attrs, validated_value)
  418. if errors:
  419. raise ValidationError(errors)
  420. return ret
  421. def to_representation(self, instance):
  422. """
  423. Object instance -> Dict of primitive datatypes.
  424. """
  425. ret = OrderedDict()
  426. fields = self._readable_fields
  427. for field in fields:
  428. try:
  429. attribute = field.get_attribute(instance)
  430. except SkipField:
  431. continue
  432. # We skip `to_representation` for `None` values so that fields do
  433. # not have to explicitly deal with that case.
  434. #
  435. # For related fields with `use_pk_only_optimization` we need to
  436. # resolve the pk value.
  437. check_for_none = attribute.pk if isinstance(attribute, PKOnlyObject) else attribute
  438. if check_for_none is None:
  439. ret[field.field_name] = None
  440. else:
  441. ret[field.field_name] = field.to_representation(attribute)
  442. return ret
  443. def validate(self, attrs):
  444. return attrs
  445. def __repr__(self):
  446. return representation.serializer_repr(self, indent=1)
  447. # The following are used for accessing `BoundField` instances on the
  448. # serializer, for the purposes of presenting a form-like API onto the
  449. # field values and field errors.
  450. def __iter__(self):
  451. for field in self.fields.values():
  452. yield self[field.field_name]
  453. def __getitem__(self, key):
  454. field = self.fields[key]
  455. value = self.data.get(key)
  456. error = self.errors.get(key) if hasattr(self, '_errors') else None
  457. if isinstance(field, Serializer):
  458. return NestedBoundField(field, value, error)
  459. if isinstance(field, JSONField):
  460. return JSONBoundField(field, value, error)
  461. return BoundField(field, value, error)
  462. # Include a backlink to the serializer class on return objects.
  463. # Allows renderers such as HTMLFormRenderer to get the full field info.
  464. @property
  465. def data(self):
  466. ret = super().data
  467. return ReturnDict(ret, serializer=self)
  468. @property
  469. def errors(self):
  470. ret = super().errors
  471. if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
  472. # Edge case. Provide a more descriptive error than
  473. # "this field may not be null", when no data is passed.
  474. detail = ErrorDetail('No data provided', code='null')
  475. ret = {api_settings.NON_FIELD_ERRORS_KEY: [detail]}
  476. return ReturnDict(ret, serializer=self)
  477. # There's some replication of `ListField` here,
  478. # but that's probably better than obfuscating the call hierarchy.
  479. class ListSerializer(BaseSerializer):
  480. child = None
  481. many = True
  482. default_error_messages = {
  483. 'not_a_list': _('Expected a list of items but got type "{input_type}".'),
  484. 'empty': _('This list may not be empty.'),
  485. 'max_length': _('Ensure this field has no more than {max_length} elements.'),
  486. 'min_length': _('Ensure this field has at least {min_length} elements.')
  487. }
  488. def __init__(self, *args, **kwargs):
  489. self.child = kwargs.pop('child', copy.deepcopy(self.child))
  490. self.allow_empty = kwargs.pop('allow_empty', True)
  491. self.max_length = kwargs.pop('max_length', None)
  492. self.min_length = kwargs.pop('min_length', None)
  493. assert self.child is not None, '`child` is a required argument.'
  494. assert not inspect.isclass(self.child), '`child` has not been instantiated.'
  495. super().__init__(*args, **kwargs)
  496. self.child.bind(field_name='', parent=self)
  497. def get_initial(self):
  498. if hasattr(self, 'initial_data'):
  499. return self.to_representation(self.initial_data)
  500. return []
  501. def get_value(self, dictionary):
  502. """
  503. Given the input dictionary, return the field value.
  504. """
  505. # We override the default field access in order to support
  506. # lists in HTML forms.
  507. if html.is_html_input(dictionary):
  508. return html.parse_html_list(dictionary, prefix=self.field_name, default=empty)
  509. return dictionary.get(self.field_name, empty)
  510. def run_validation(self, data=empty):
  511. """
  512. We override the default `run_validation`, because the validation
  513. performed by validators and the `.validate()` method should
  514. be coerced into an error dictionary with a 'non_fields_error' key.
  515. """
  516. (is_empty_value, data) = self.validate_empty_values(data)
  517. if is_empty_value:
  518. return data
  519. value = self.to_internal_value(data)
  520. try:
  521. self.run_validators(value)
  522. value = self.validate(value)
  523. assert value is not None, '.validate() should return the validated data'
  524. except (ValidationError, DjangoValidationError) as exc:
  525. raise ValidationError(detail=as_serializer_error(exc))
  526. return value
  527. def to_internal_value(self, data):
  528. """
  529. List of dicts of native values <- List of dicts of primitive datatypes.
  530. """
  531. if html.is_html_input(data):
  532. data = html.parse_html_list(data, default=[])
  533. if not isinstance(data, list):
  534. message = self.error_messages['not_a_list'].format(
  535. input_type=type(data).__name__
  536. )
  537. raise ValidationError({
  538. api_settings.NON_FIELD_ERRORS_KEY: [message]
  539. }, code='not_a_list')
  540. if not self.allow_empty and len(data) == 0:
  541. message = self.error_messages['empty']
  542. raise ValidationError({
  543. api_settings.NON_FIELD_ERRORS_KEY: [message]
  544. }, code='empty')
  545. if self.max_length is not None and len(data) > self.max_length:
  546. message = self.error_messages['max_length'].format(max_length=self.max_length)
  547. raise ValidationError({
  548. api_settings.NON_FIELD_ERRORS_KEY: [message]
  549. }, code='max_length')
  550. if self.min_length is not None and len(data) < self.min_length:
  551. message = self.error_messages['min_length'].format(min_length=self.min_length)
  552. raise ValidationError({
  553. api_settings.NON_FIELD_ERRORS_KEY: [message]
  554. }, code='min_length')
  555. ret = []
  556. errors = []
  557. for item in data:
  558. try:
  559. validated = self.child.run_validation(item)
  560. except ValidationError as exc:
  561. errors.append(exc.detail)
  562. else:
  563. ret.append(validated)
  564. errors.append({})
  565. if any(errors):
  566. raise ValidationError(errors)
  567. return ret
  568. def to_representation(self, data):
  569. """
  570. List of object instances -> List of dicts of primitive datatypes.
  571. """
  572. # Dealing with nested relationships, data can be a Manager,
  573. # so, first get a queryset from the Manager if needed
  574. iterable = data.all() if isinstance(data, models.Manager) else data
  575. return [
  576. self.child.to_representation(item) for item in iterable
  577. ]
  578. def validate(self, attrs):
  579. return attrs
  580. def update(self, instance, validated_data):
  581. raise NotImplementedError(
  582. "Serializers with many=True do not support multiple update by "
  583. "default, only multiple create. For updates it is unclear how to "
  584. "deal with insertions and deletions. If you need to support "
  585. "multiple update, use a `ListSerializer` class and override "
  586. "`.update()` so you can specify the behavior exactly."
  587. )
  588. def create(self, validated_data):
  589. return [
  590. self.child.create(attrs) for attrs in validated_data
  591. ]
  592. def save(self, **kwargs):
  593. """
  594. Save and return a list of object instances.
  595. """
  596. # Guard against incorrect use of `serializer.save(commit=False)`
  597. assert 'commit' not in kwargs, (
  598. "'commit' is not a valid keyword argument to the 'save()' method. "
  599. "If you need to access data before committing to the database then "
  600. "inspect 'serializer.validated_data' instead. "
  601. "You can also pass additional keyword arguments to 'save()' if you "
  602. "need to set extra attributes on the saved model instance. "
  603. "For example: 'serializer.save(owner=request.user)'.'"
  604. )
  605. validated_data = [
  606. {**attrs, **kwargs} for attrs in self.validated_data
  607. ]
  608. if self.instance is not None:
  609. self.instance = self.update(self.instance, validated_data)
  610. assert self.instance is not None, (
  611. '`update()` did not return an object instance.'
  612. )
  613. else:
  614. self.instance = self.create(validated_data)
  615. assert self.instance is not None, (
  616. '`create()` did not return an object instance.'
  617. )
  618. return self.instance
  619. def is_valid(self, raise_exception=False):
  620. # This implementation is the same as the default,
  621. # except that we use lists, rather than dicts, as the empty case.
  622. assert hasattr(self, 'initial_data'), (
  623. 'Cannot call `.is_valid()` as no `data=` keyword argument was '
  624. 'passed when instantiating the serializer instance.'
  625. )
  626. if not hasattr(self, '_validated_data'):
  627. try:
  628. self._validated_data = self.run_validation(self.initial_data)
  629. except ValidationError as exc:
  630. self._validated_data = []
  631. self._errors = exc.detail
  632. else:
  633. self._errors = []
  634. if self._errors and raise_exception:
  635. raise ValidationError(self.errors)
  636. return not bool(self._errors)
  637. def __repr__(self):
  638. return representation.list_repr(self, indent=1)
  639. # Include a backlink to the serializer class on return objects.
  640. # Allows renderers such as HTMLFormRenderer to get the full field info.
  641. @property
  642. def data(self):
  643. ret = super().data
  644. return ReturnList(ret, serializer=self)
  645. @property
  646. def errors(self):
  647. ret = super().errors
  648. if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
  649. # Edge case. Provide a more descriptive error than
  650. # "this field may not be null", when no data is passed.
  651. detail = ErrorDetail('No data provided', code='null')
  652. ret = {api_settings.NON_FIELD_ERRORS_KEY: [detail]}
  653. if isinstance(ret, dict):
  654. return ReturnDict(ret, serializer=self)
  655. return ReturnList(ret, serializer=self)
  656. # ModelSerializer & HyperlinkedModelSerializer
  657. # --------------------------------------------
  658. def raise_errors_on_nested_writes(method_name, serializer, validated_data):
  659. """
  660. Give explicit errors when users attempt to pass writable nested data.
  661. If we don't do this explicitly they'd get a less helpful error when
  662. calling `.save()` on the serializer.
  663. We don't *automatically* support these sorts of nested writes because
  664. there are too many ambiguities to define a default behavior.
  665. Eg. Suppose we have a `UserSerializer` with a nested profile. How should
  666. we handle the case of an update, where the `profile` relationship does
  667. not exist? Any of the following might be valid:
  668. * Raise an application error.
  669. * Silently ignore the nested part of the update.
  670. * Automatically create a profile instance.
  671. """
  672. ModelClass = serializer.Meta.model
  673. model_field_info = model_meta.get_field_info(ModelClass)
  674. # Ensure we don't have a writable nested field. For example:
  675. #
  676. # class UserSerializer(ModelSerializer):
  677. # ...
  678. # profile = ProfileSerializer()
  679. assert not any(
  680. isinstance(field, BaseSerializer) and
  681. (field.source in validated_data) and
  682. (field.source in model_field_info.relations) and
  683. isinstance(validated_data[field.source], (list, dict))
  684. for field in serializer._writable_fields
  685. ), (
  686. 'The `.{method_name}()` method does not support writable nested '
  687. 'fields by default.\nWrite an explicit `.{method_name}()` method for '
  688. 'serializer `{module}.{class_name}`, or set `read_only=True` on '
  689. 'nested serializer fields.'.format(
  690. method_name=method_name,
  691. module=serializer.__class__.__module__,
  692. class_name=serializer.__class__.__name__
  693. )
  694. )
  695. # Ensure we don't have a writable dotted-source field. For example:
  696. #
  697. # class UserSerializer(ModelSerializer):
  698. # ...
  699. # address = serializer.CharField('profile.address')
  700. #
  701. # Though, non-relational fields (e.g., JSONField) are acceptable. For example:
  702. #
  703. # class NonRelationalPersonModel(models.Model):
  704. # profile = JSONField()
  705. #
  706. # class UserSerializer(ModelSerializer):
  707. # ...
  708. # address = serializer.CharField('profile.address')
  709. assert not any(
  710. len(field.source_attrs) > 1 and
  711. (field.source_attrs[0] in validated_data) and
  712. (field.source_attrs[0] in model_field_info.relations) and
  713. isinstance(validated_data[field.source_attrs[0]], (list, dict))
  714. for field in serializer._writable_fields
  715. ), (
  716. 'The `.{method_name}()` method does not support writable dotted-source '
  717. 'fields by default.\nWrite an explicit `.{method_name}()` method for '
  718. 'serializer `{module}.{class_name}`, or set `read_only=True` on '
  719. 'dotted-source serializer fields.'.format(
  720. method_name=method_name,
  721. module=serializer.__class__.__module__,
  722. class_name=serializer.__class__.__name__
  723. )
  724. )
  725. class ModelSerializer(Serializer):
  726. """
  727. A `ModelSerializer` is just a regular `Serializer`, except that:
  728. * A set of default fields are automatically populated.
  729. * A set of default validators are automatically populated.
  730. * Default `.create()` and `.update()` implementations are provided.
  731. The process of automatically determining a set of serializer fields
  732. based on the model fields is reasonably complex, but you almost certainly
  733. don't need to dig into the implementation.
  734. If the `ModelSerializer` class *doesn't* generate the set of fields that
  735. you need you should either declare the extra/differing fields explicitly on
  736. the serializer class, or simply use a `Serializer` class.
  737. """
  738. serializer_field_mapping = {
  739. models.AutoField: IntegerField,
  740. models.BigIntegerField: IntegerField,
  741. models.BooleanField: BooleanField,
  742. models.CharField: CharField,
  743. models.CommaSeparatedIntegerField: CharField,
  744. models.DateField: DateField,
  745. models.DateTimeField: DateTimeField,
  746. models.DecimalField: DecimalField,
  747. models.DurationField: DurationField,
  748. models.EmailField: EmailField,
  749. models.Field: ModelField,
  750. models.FileField: FileField,
  751. models.FloatField: FloatField,
  752. models.ImageField: ImageField,
  753. models.IntegerField: IntegerField,
  754. models.NullBooleanField: BooleanField,
  755. models.PositiveIntegerField: IntegerField,
  756. models.PositiveSmallIntegerField: IntegerField,
  757. models.SlugField: SlugField,
  758. models.SmallIntegerField: IntegerField,
  759. models.TextField: CharField,
  760. models.TimeField: TimeField,
  761. models.URLField: URLField,
  762. models.UUIDField: UUIDField,
  763. models.GenericIPAddressField: IPAddressField,
  764. models.FilePathField: FilePathField,
  765. }
  766. if hasattr(models, 'JSONField'):
  767. serializer_field_mapping[models.JSONField] = JSONField
  768. if postgres_fields:
  769. serializer_field_mapping[postgres_fields.HStoreField] = HStoreField
  770. serializer_field_mapping[postgres_fields.ArrayField] = ListField
  771. serializer_field_mapping[postgres_fields.JSONField] = JSONField
  772. serializer_related_field = PrimaryKeyRelatedField
  773. serializer_related_to_field = SlugRelatedField
  774. serializer_url_field = HyperlinkedIdentityField
  775. serializer_choice_field = ChoiceField
  776. # The field name for hyperlinked identity fields. Defaults to 'url'.
  777. # You can modify this using the API setting.
  778. #
  779. # Note that if you instead need modify this on a per-serializer basis,
  780. # you'll also need to ensure you update the `create` method on any generic
  781. # views, to correctly handle the 'Location' response header for
  782. # "HTTP 201 Created" responses.
  783. url_field_name = None
  784. # Default `create` and `update` behavior...
  785. def create(self, validated_data):
  786. """
  787. We have a bit of extra checking around this in order to provide
  788. descriptive messages when something goes wrong, but this method is
  789. essentially just:
  790. return ExampleModel.objects.create(**validated_data)
  791. If there are many to many fields present on the instance then they
  792. cannot be set until the model is instantiated, in which case the
  793. implementation is like so:
  794. example_relationship = validated_data.pop('example_relationship')
  795. instance = ExampleModel.objects.create(**validated_data)
  796. instance.example_relationship = example_relationship
  797. return instance
  798. The default implementation also does not handle nested relationships.
  799. If you want to support writable nested relationships you'll need
  800. to write an explicit `.create()` method.
  801. """
  802. raise_errors_on_nested_writes('create', self, validated_data)
  803. ModelClass = self.Meta.model
  804. # Remove many-to-many relationships from validated_data.
  805. # They are not valid arguments to the default `.create()` method,
  806. # as they require that the instance has already been saved.
  807. info = model_meta.get_field_info(ModelClass)
  808. many_to_many = {}
  809. for field_name, relation_info in info.relations.items():
  810. if relation_info.to_many and (field_name in validated_data):
  811. many_to_many[field_name] = validated_data.pop(field_name)
  812. try:
  813. instance = ModelClass._default_manager.create(**validated_data)
  814. except TypeError:
  815. tb = traceback.format_exc()
  816. msg = (
  817. 'Got a `TypeError` when calling `%s.%s.create()`. '
  818. 'This may be because you have a writable field on the '
  819. 'serializer class that is not a valid argument to '
  820. '`%s.%s.create()`. You may need to make the field '
  821. 'read-only, or override the %s.create() method to handle '
  822. 'this correctly.\nOriginal exception was:\n %s' %
  823. (
  824. ModelClass.__name__,
  825. ModelClass._default_manager.name,
  826. ModelClass.__name__,
  827. ModelClass._default_manager.name,
  828. self.__class__.__name__,
  829. tb
  830. )
  831. )
  832. raise TypeError(msg)
  833. # Save many-to-many relationships after the instance is created.
  834. if many_to_many:
  835. for field_name, value in many_to_many.items():
  836. field = getattr(instance, field_name)
  837. field.set(value)
  838. return instance
  839. def update(self, instance, validated_data):
  840. raise_errors_on_nested_writes('update', self, validated_data)
  841. info = model_meta.get_field_info(instance)
  842. # Simply set each attribute on the instance, and then save it.
  843. # Note that unlike `.create()` we don't need to treat many-to-many
  844. # relationships as being a special case. During updates we already
  845. # have an instance pk for the relationships to be associated with.
  846. m2m_fields = []
  847. for attr, value in validated_data.items():
  848. if attr in info.relations and info.relations[attr].to_many:
  849. m2m_fields.append((attr, value))
  850. else:
  851. setattr(instance, attr, value)
  852. instance.save()
  853. # Note that many-to-many fields are set after updating instance.
  854. # Setting m2m fields triggers signals which could potentially change
  855. # updated instance and we do not want it to collide with .update()
  856. for attr, value in m2m_fields:
  857. field = getattr(instance, attr)
  858. field.set(value)
  859. return instance
  860. # Determine the fields to apply...
  861. def get_fields(self):
  862. """
  863. Return the dict of field names -> field instances that should be
  864. used for `self.fields` when instantiating the serializer.
  865. """
  866. if self.url_field_name is None:
  867. self.url_field_name = api_settings.URL_FIELD_NAME
  868. assert hasattr(self, 'Meta'), (
  869. 'Class {serializer_class} missing "Meta" attribute'.format(
  870. serializer_class=self.__class__.__name__
  871. )
  872. )
  873. assert hasattr(self.Meta, 'model'), (
  874. 'Class {serializer_class} missing "Meta.model" attribute'.format(
  875. serializer_class=self.__class__.__name__
  876. )
  877. )
  878. if model_meta.is_abstract_model(self.Meta.model):
  879. raise ValueError(
  880. 'Cannot use ModelSerializer with Abstract Models.'
  881. )
  882. declared_fields = copy.deepcopy(self._declared_fields)
  883. model = getattr(self.Meta, 'model')
  884. depth = getattr(self.Meta, 'depth', 0)
  885. if depth is not None:
  886. assert depth >= 0, "'depth' may not be negative."
  887. assert depth <= 10, "'depth' may not be greater than 10."
  888. # Retrieve metadata about fields & relationships on the model class.
  889. info = model_meta.get_field_info(model)
  890. field_names = self.get_field_names(declared_fields, info)
  891. # Determine any extra field arguments and hidden fields that
  892. # should be included
  893. extra_kwargs = self.get_extra_kwargs()
  894. extra_kwargs, hidden_fields = self.get_uniqueness_extra_kwargs(
  895. field_names, declared_fields, extra_kwargs
  896. )
  897. # Determine the fields that should be included on the serializer.
  898. fields = OrderedDict()
  899. for field_name in field_names:
  900. # If the field is explicitly declared on the class then use that.
  901. if field_name in declared_fields:
  902. fields[field_name] = declared_fields[field_name]
  903. continue
  904. extra_field_kwargs = extra_kwargs.get(field_name, {})
  905. source = extra_field_kwargs.get('source', '*')
  906. if source == '*':
  907. source = field_name
  908. # Determine the serializer field class and keyword arguments.
  909. field_class, field_kwargs = self.build_field(
  910. source, info, model, depth
  911. )
  912. # Include any kwargs defined in `Meta.extra_kwargs`
  913. field_kwargs = self.include_extra_kwargs(
  914. field_kwargs, extra_field_kwargs
  915. )
  916. # Create the serializer field.
  917. fields[field_name] = field_class(**field_kwargs)
  918. # Add in any hidden fields.
  919. fields.update(hidden_fields)
  920. return fields
  921. # Methods for determining the set of field names to include...
  922. def get_field_names(self, declared_fields, info):
  923. """
  924. Returns the list of all field names that should be created when
  925. instantiating this serializer class. This is based on the default
  926. set of fields, but also takes into account the `Meta.fields` or
  927. `Meta.exclude` options if they have been specified.
  928. """
  929. fields = getattr(self.Meta, 'fields', None)
  930. exclude = getattr(self.Meta, 'exclude', None)
  931. if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
  932. raise TypeError(
  933. 'The `fields` option must be a list or tuple or "__all__". '
  934. 'Got %s.' % type(fields).__name__
  935. )
  936. if exclude and not isinstance(exclude, (list, tuple)):
  937. raise TypeError(
  938. 'The `exclude` option must be a list or tuple. Got %s.' %
  939. type(exclude).__name__
  940. )
  941. assert not (fields and exclude), (
  942. "Cannot set both 'fields' and 'exclude' options on "
  943. "serializer {serializer_class}.".format(
  944. serializer_class=self.__class__.__name__
  945. )
  946. )
  947. assert not (fields is None and exclude is None), (
  948. "Creating a ModelSerializer without either the 'fields' attribute "
  949. "or the 'exclude' attribute has been deprecated since 3.3.0, "
  950. "and is now disallowed. Add an explicit fields = '__all__' to the "
  951. "{serializer_class} serializer.".format(
  952. serializer_class=self.__class__.__name__
  953. ),
  954. )
  955. if fields == ALL_FIELDS:
  956. fields = None
  957. if fields is not None:
  958. # Ensure that all declared fields have also been included in the
  959. # `Meta.fields` option.
  960. # Do not require any fields that are declared in a parent class,
  961. # in order to allow serializer subclasses to only include
  962. # a subset of fields.
  963. required_field_names = set(declared_fields)
  964. for cls in self.__class__.__bases__:
  965. required_field_names -= set(getattr(cls, '_declared_fields', []))
  966. for field_name in required_field_names:
  967. assert field_name in fields, (
  968. "The field '{field_name}' was declared on serializer "
  969. "{serializer_class}, but has not been included in the "
  970. "'fields' option.".format(
  971. field_name=field_name,
  972. serializer_class=self.__class__.__name__
  973. )
  974. )
  975. return fields
  976. # Use the default set of field names if `Meta.fields` is not specified.
  977. fields = self.get_default_field_names(declared_fields, info)
  978. if exclude is not None:
  979. # If `Meta.exclude` is included, then remove those fields.
  980. for field_name in exclude:
  981. assert field_name not in self._declared_fields, (
  982. "Cannot both declare the field '{field_name}' and include "
  983. "it in the {serializer_class} 'exclude' option. Remove the "
  984. "field or, if inherited from a parent serializer, disable "
  985. "with `{field_name} = None`."
  986. .format(
  987. field_name=field_name,
  988. serializer_class=self.__class__.__name__
  989. )
  990. )
  991. assert field_name in fields, (
  992. "The field '{field_name}' was included on serializer "
  993. "{serializer_class} in the 'exclude' option, but does "
  994. "not match any model field.".format(
  995. field_name=field_name,
  996. serializer_class=self.__class__.__name__
  997. )
  998. )
  999. fields.remove(field_name)
  1000. return fields
  1001. def get_default_field_names(self, declared_fields, model_info):
  1002. """
  1003. Return the default list of field names that will be used if the
  1004. `Meta.fields` option is not specified.
  1005. """
  1006. return (
  1007. [model_info.pk.name] +
  1008. list(declared_fields) +
  1009. list(model_info.fields) +
  1010. list(model_info.forward_relations)
  1011. )
  1012. # Methods for constructing serializer fields...
  1013. def build_field(self, field_name, info, model_class, nested_depth):
  1014. """
  1015. Return a two tuple of (cls, kwargs) to build a serializer field with.
  1016. """
  1017. if field_name in info.fields_and_pk:
  1018. model_field = info.fields_and_pk[field_name]
  1019. return self.build_standard_field(field_name, model_field)
  1020. elif field_name in info.relations:
  1021. relation_info = info.relations[field_name]
  1022. if not nested_depth:
  1023. return self.build_relational_field(field_name, relation_info)
  1024. else:
  1025. return self.build_nested_field(field_name, relation_info, nested_depth)
  1026. elif hasattr(model_class, field_name):
  1027. return self.build_property_field(field_name, model_class)
  1028. elif field_name == self.url_field_name:
  1029. return self.build_url_field(field_name, model_class)
  1030. return self.build_unknown_field(field_name, model_class)
  1031. def build_standard_field(self, field_name, model_field):
  1032. """
  1033. Create regular model fields.
  1034. """
  1035. field_mapping = ClassLookupDict(self.serializer_field_mapping)
  1036. field_class = field_mapping[model_field]
  1037. field_kwargs = get_field_kwargs(field_name, model_field)
  1038. # Special case to handle when a OneToOneField is also the primary key
  1039. if model_field.one_to_one and model_field.primary_key:
  1040. field_class = self.serializer_related_field
  1041. field_kwargs['queryset'] = model_field.related_model.objects
  1042. if 'choices' in field_kwargs:
  1043. # Fields with choices get coerced into `ChoiceField`
  1044. # instead of using their regular typed field.
  1045. field_class = self.serializer_choice_field
  1046. # Some model fields may introduce kwargs that would not be valid
  1047. # for the choice field. We need to strip these out.
  1048. # Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
  1049. valid_kwargs = {
  1050. 'read_only', 'write_only',
  1051. 'required', 'default', 'initial', 'source',
  1052. 'label', 'help_text', 'style',
  1053. 'error_messages', 'validators', 'allow_null', 'allow_blank',
  1054. 'choices'
  1055. }
  1056. for key in list(field_kwargs):
  1057. if key not in valid_kwargs:
  1058. field_kwargs.pop(key)
  1059. if not issubclass(field_class, ModelField):
  1060. # `model_field` is only valid for the fallback case of
  1061. # `ModelField`, which is used when no other typed field
  1062. # matched to the model field.
  1063. field_kwargs.pop('model_field', None)
  1064. if not issubclass(field_class, CharField) and not issubclass(field_class, ChoiceField):
  1065. # `allow_blank` is only valid for textual fields.
  1066. field_kwargs.pop('allow_blank', None)
  1067. is_django_jsonfield = hasattr(models, 'JSONField') and isinstance(model_field, models.JSONField)
  1068. if (postgres_fields and isinstance(model_field, postgres_fields.JSONField)) or is_django_jsonfield:
  1069. # Populate the `encoder` argument of `JSONField` instances generated
  1070. # for the model `JSONField`.
  1071. field_kwargs['encoder'] = getattr(model_field, 'encoder', None)
  1072. if is_django_jsonfield:
  1073. field_kwargs['decoder'] = getattr(model_field, 'decoder', None)
  1074. if postgres_fields and isinstance(model_field, postgres_fields.ArrayField):
  1075. # Populate the `child` argument on `ListField` instances generated
  1076. # for the PostgreSQL specific `ArrayField`.
  1077. child_model_field = model_field.base_field
  1078. child_field_class, child_field_kwargs = self.build_standard_field(
  1079. 'child', child_model_field
  1080. )
  1081. field_kwargs['child'] = child_field_class(**child_field_kwargs)
  1082. return field_class, field_kwargs
  1083. def build_relational_field(self, field_name, relation_info):
  1084. """
  1085. Create fields for forward and reverse relationships.
  1086. """
  1087. field_class = self.serializer_related_field
  1088. field_kwargs = get_relation_kwargs(field_name, relation_info)
  1089. to_field = field_kwargs.pop('to_field', None)
  1090. if to_field and not relation_info.reverse and not relation_info.related_model._meta.get_field(to_field).primary_key:
  1091. field_kwargs['slug_field'] = to_field
  1092. field_class = self.serializer_related_to_field
  1093. # `view_name` is only valid for hyperlinked relationships.
  1094. if not issubclass(field_class, HyperlinkedRelatedField):
  1095. field_kwargs.pop('view_name', None)
  1096. return field_class, field_kwargs
  1097. def build_nested_field(self, field_name, relation_info, nested_depth):
  1098. """
  1099. Create nested fields for forward and reverse relationships.
  1100. """
  1101. class NestedSerializer(ModelSerializer):
  1102. class Meta:
  1103. model = relation_info.related_model
  1104. depth = nested_depth - 1
  1105. fields = '__all__'
  1106. field_class = NestedSerializer
  1107. field_kwargs = get_nested_relation_kwargs(relation_info)
  1108. return field_class, field_kwargs
  1109. def build_property_field(self, field_name, model_class):
  1110. """
  1111. Create a read only field for model methods and properties.
  1112. """
  1113. field_class = ReadOnlyField
  1114. field_kwargs = {}
  1115. return field_class, field_kwargs
  1116. def build_url_field(self, field_name, model_class):
  1117. """
  1118. Create a field representing the object's own URL.
  1119. """
  1120. field_class = self.serializer_url_field
  1121. field_kwargs = get_url_kwargs(model_class)
  1122. return field_class, field_kwargs
  1123. def build_unknown_field(self, field_name, model_class):
  1124. """
  1125. Raise an error on any unknown fields.
  1126. """
  1127. raise ImproperlyConfigured(
  1128. 'Field name `%s` is not valid for model `%s`.' %
  1129. (field_name, model_class.__name__)
  1130. )
  1131. def include_extra_kwargs(self, kwargs, extra_kwargs):
  1132. """
  1133. Include any 'extra_kwargs' that have been included for this field,
  1134. possibly removing any incompatible existing keyword arguments.
  1135. """
  1136. if extra_kwargs.get('read_only', False):
  1137. for attr in [
  1138. 'required', 'default', 'allow_blank', 'min_length',
  1139. 'max_length', 'min_value', 'max_value', 'validators', 'queryset'
  1140. ]:
  1141. kwargs.pop(attr, None)
  1142. if extra_kwargs.get('default') and kwargs.get('required') is False:
  1143. kwargs.pop('required')
  1144. if extra_kwargs.get('read_only', kwargs.get('read_only', False)):
  1145. extra_kwargs.pop('required', None) # Read only fields should always omit the 'required' argument.
  1146. kwargs.update(extra_kwargs)
  1147. return kwargs
  1148. # Methods for determining additional keyword arguments to apply...
  1149. def get_extra_kwargs(self):
  1150. """
  1151. Return a dictionary mapping field names to a dictionary of
  1152. additional keyword arguments.
  1153. """
  1154. extra_kwargs = copy.deepcopy(getattr(self.Meta, 'extra_kwargs', {}))
  1155. read_only_fields = getattr(self.Meta, 'read_only_fields', None)
  1156. if read_only_fields is not None:
  1157. if not isinstance(read_only_fields, (list, tuple)):
  1158. raise TypeError(
  1159. 'The `read_only_fields` option must be a list or tuple. '
  1160. 'Got %s.' % type(read_only_fields).__name__
  1161. )
  1162. for field_name in read_only_fields:
  1163. kwargs = extra_kwargs.get(field_name, {})
  1164. kwargs['read_only'] = True
  1165. extra_kwargs[field_name] = kwargs
  1166. else:
  1167. # Guard against the possible misspelling `readonly_fields` (used
  1168. # by the Django admin and others).
  1169. assert not hasattr(self.Meta, 'readonly_fields'), (
  1170. 'Serializer `%s.%s` has field `readonly_fields`; '
  1171. 'the correct spelling for the option is `read_only_fields`.' %
  1172. (self.__class__.__module__, self.__class__.__name__)
  1173. )
  1174. return extra_kwargs
  1175. def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs):
  1176. """
  1177. Return any additional field options that need to be included as a
  1178. result of uniqueness constraints on the model. This is returned as
  1179. a two-tuple of:
  1180. ('dict of updated extra kwargs', 'mapping of hidden fields')
  1181. """
  1182. if getattr(self.Meta, 'validators', None) is not None:
  1183. return (extra_kwargs, {})
  1184. model = getattr(self.Meta, 'model')
  1185. model_fields = self._get_model_fields(
  1186. field_names, declared_fields, extra_kwargs
  1187. )
  1188. # Determine if we need any additional `HiddenField` or extra keyword
  1189. # arguments to deal with `unique_for` dates that are required to
  1190. # be in the input data in order to validate it.
  1191. unique_constraint_names = set()
  1192. for model_field in model_fields.values():
  1193. # Include each of the `unique_for_*` field names.
  1194. unique_constraint_names |= {model_field.unique_for_date, model_field.unique_for_month,
  1195. model_field.unique_for_year}
  1196. unique_constraint_names -= {None}
  1197. # Include each of the `unique_together` field names,
  1198. # so long as all the field names are included on the serializer.
  1199. for parent_class in [model] + list(model._meta.parents):
  1200. for unique_together_list in parent_class._meta.unique_together:
  1201. if set(field_names).issuperset(unique_together_list):
  1202. unique_constraint_names |= set(unique_together_list)
  1203. # Now we have all the field names that have uniqueness constraints
  1204. # applied, we can add the extra 'required=...' or 'default=...'
  1205. # arguments that are appropriate to these fields, or add a `HiddenField` for it.
  1206. hidden_fields = {}
  1207. uniqueness_extra_kwargs = {}
  1208. for unique_constraint_name in unique_constraint_names:
  1209. # Get the model field that is referred too.
  1210. unique_constraint_field = model._meta.get_field(unique_constraint_name)
  1211. if getattr(unique_constraint_field, 'auto_now_add', None):
  1212. default = CreateOnlyDefault(timezone.now)
  1213. elif getattr(unique_constraint_field, 'auto_now', None):
  1214. default = timezone.now
  1215. elif unique_constraint_field.has_default():
  1216. default = unique_constraint_field.default
  1217. else:
  1218. default = empty
  1219. if unique_constraint_name in model_fields:
  1220. # The corresponding field is present in the serializer
  1221. if default is empty:
  1222. uniqueness_extra_kwargs[unique_constraint_name] = {'required': True}
  1223. else:
  1224. uniqueness_extra_kwargs[unique_constraint_name] = {'default': default}
  1225. elif default is not empty:
  1226. # The corresponding field is not present in the
  1227. # serializer. We have a default to use for it, so
  1228. # add in a hidden field that populates it.
  1229. hidden_fields[unique_constraint_name] = HiddenField(default=default)
  1230. # Update `extra_kwargs` with any new options.
  1231. for key, value in uniqueness_extra_kwargs.items():
  1232. if key in extra_kwargs:
  1233. value.update(extra_kwargs[key])
  1234. extra_kwargs[key] = value
  1235. return extra_kwargs, hidden_fields
  1236. def _get_model_fields(self, field_names, declared_fields, extra_kwargs):
  1237. """
  1238. Returns all the model fields that are being mapped to by fields
  1239. on the serializer class.
  1240. Returned as a dict of 'model field name' -> 'model field'.
  1241. Used internally by `get_uniqueness_field_options`.
  1242. """
  1243. model = getattr(self.Meta, 'model')
  1244. model_fields = {}
  1245. for field_name in field_names:
  1246. if field_name in declared_fields:
  1247. # If the field is declared on the serializer
  1248. field = declared_fields[field_name]
  1249. source = field.source or field_name
  1250. else:
  1251. try:
  1252. source = extra_kwargs[field_name]['source']
  1253. except KeyError:
  1254. source = field_name
  1255. if '.' in source or source == '*':
  1256. # Model fields will always have a simple source mapping,
  1257. # they can't be nested attribute lookups.
  1258. continue
  1259. try:
  1260. field = model._meta.get_field(source)
  1261. if isinstance(field, DjangoModelField):
  1262. model_fields[source] = field
  1263. except FieldDoesNotExist:
  1264. pass
  1265. return model_fields
  1266. # Determine the validators to apply...
  1267. def get_validators(self):
  1268. """
  1269. Determine the set of validators to use when instantiating serializer.
  1270. """
  1271. # If the validators have been declared explicitly then use that.
  1272. validators = getattr(getattr(self, 'Meta', None), 'validators', None)
  1273. if validators is not None:
  1274. return list(validators)
  1275. # Otherwise use the default set of validators.
  1276. return (
  1277. self.get_unique_together_validators() +
  1278. self.get_unique_for_date_validators()
  1279. )
  1280. def get_unique_together_validators(self):
  1281. """
  1282. Determine a default set of validators for any unique_together constraints.
  1283. """
  1284. model_class_inheritance_tree = (
  1285. [self.Meta.model] +
  1286. list(self.Meta.model._meta.parents)
  1287. )
  1288. # The field names we're passing though here only include fields
  1289. # which may map onto a model field. Any dotted field name lookups
  1290. # cannot map to a field, and must be a traversal, so we're not
  1291. # including those.
  1292. field_sources = OrderedDict(
  1293. (field.field_name, field.source) for field in self._writable_fields
  1294. if (field.source != '*') and ('.' not in field.source)
  1295. )
  1296. # Special Case: Add read_only fields with defaults.
  1297. field_sources.update(OrderedDict(
  1298. (field.field_name, field.source) for field in self.fields.values()
  1299. if (field.read_only) and (field.default != empty) and (field.source != '*') and ('.' not in field.source)
  1300. ))
  1301. # Invert so we can find the serializer field names that correspond to
  1302. # the model field names in the unique_together sets. This also allows
  1303. # us to check that multiple fields don't map to the same source.
  1304. source_map = defaultdict(list)
  1305. for name, source in field_sources.items():
  1306. source_map[source].append(name)
  1307. # Note that we make sure to check `unique_together` both on the
  1308. # base model class, but also on any parent classes.
  1309. validators = []
  1310. for parent_class in model_class_inheritance_tree:
  1311. for unique_together in parent_class._meta.unique_together:
  1312. # Skip if serializer does not map to all unique together sources
  1313. if not set(source_map).issuperset(unique_together):
  1314. continue
  1315. for source in unique_together:
  1316. assert len(source_map[source]) == 1, (
  1317. "Unable to create `UniqueTogetherValidator` for "
  1318. "`{model}.{field}` as `{serializer}` has multiple "
  1319. "fields ({fields}) that map to this model field. "
  1320. "Either remove the extra fields, or override "
  1321. "`Meta.validators` with a `UniqueTogetherValidator` "
  1322. "using the desired field names."
  1323. .format(
  1324. model=self.Meta.model.__name__,
  1325. serializer=self.__class__.__name__,
  1326. field=source,
  1327. fields=', '.join(source_map[source]),
  1328. )
  1329. )
  1330. field_names = tuple(source_map[f][0] for f in unique_together)
  1331. validator = UniqueTogetherValidator(
  1332. queryset=parent_class._default_manager,
  1333. fields=field_names
  1334. )
  1335. validators.append(validator)
  1336. return validators
  1337. def get_unique_for_date_validators(self):
  1338. """
  1339. Determine a default set of validators for the following constraints:
  1340. * unique_for_date
  1341. * unique_for_month
  1342. * unique_for_year
  1343. """
  1344. info = model_meta.get_field_info(self.Meta.model)
  1345. default_manager = self.Meta.model._default_manager
  1346. field_names = [field.source for field in self.fields.values()]
  1347. validators = []
  1348. for field_name, field in info.fields_and_pk.items():
  1349. if field.unique_for_date and field_name in field_names:
  1350. validator = UniqueForDateValidator(
  1351. queryset=default_manager,
  1352. field=field_name,
  1353. date_field=field.unique_for_date
  1354. )
  1355. validators.append(validator)
  1356. if field.unique_for_month and field_name in field_names:
  1357. validator = UniqueForMonthValidator(
  1358. queryset=default_manager,
  1359. field=field_name,
  1360. date_field=field.unique_for_month
  1361. )
  1362. validators.append(validator)
  1363. if field.unique_for_year and field_name in field_names:
  1364. validator = UniqueForYearValidator(
  1365. queryset=default_manager,
  1366. field=field_name,
  1367. date_field=field.unique_for_year
  1368. )
  1369. validators.append(validator)
  1370. return validators
  1371. class HyperlinkedModelSerializer(ModelSerializer):
  1372. """
  1373. A type of `ModelSerializer` that uses hyperlinked relationships instead
  1374. of primary key relationships. Specifically:
  1375. * A 'url' field is included instead of the 'id' field.
  1376. * Relationships to other instances are hyperlinks, instead of primary keys.
  1377. """
  1378. serializer_related_field = HyperlinkedRelatedField
  1379. def get_default_field_names(self, declared_fields, model_info):
  1380. """
  1381. Return the default list of field names that will be used if the
  1382. `Meta.fields` option is not specified.
  1383. """
  1384. return (
  1385. [self.url_field_name] +
  1386. list(declared_fields) +
  1387. list(model_info.fields) +
  1388. list(model_info.forward_relations)
  1389. )
  1390. def build_nested_field(self, field_name, relation_info, nested_depth):
  1391. """
  1392. Create nested fields for forward and reverse relationships.
  1393. """
  1394. class NestedSerializer(HyperlinkedModelSerializer):
  1395. class Meta:
  1396. model = relation_info.related_model
  1397. depth = nested_depth - 1
  1398. fields = '__all__'
  1399. field_class = NestedSerializer
  1400. field_kwargs = get_nested_relation_kwargs(relation_info)
  1401. return field_class, field_kwargs