serializers.py 63 KB

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