relations.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import sys
  2. from collections import OrderedDict
  3. from urllib import parse
  4. from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
  5. from django.db.models import Manager
  6. from django.db.models.query import QuerySet
  7. from django.urls import NoReverseMatch, Resolver404, get_script_prefix, resolve
  8. from django.utils.encoding import smart_str, uri_to_iri
  9. from django.utils.translation import gettext_lazy as _
  10. from rest_framework.fields import (
  11. Field, empty, get_attribute, is_simple_callable, iter_options
  12. )
  13. from rest_framework.reverse import reverse
  14. from rest_framework.settings import api_settings
  15. from rest_framework.utils import html
  16. def method_overridden(method_name, klass, instance):
  17. """
  18. Determine if a method has been overridden.
  19. """
  20. method = getattr(klass, method_name)
  21. default_method = getattr(method, '__func__', method) # Python 3 compat
  22. return default_method is not getattr(instance, method_name).__func__
  23. class ObjectValueError(ValueError):
  24. """
  25. Raised when `queryset.get()` failed due to an underlying `ValueError`.
  26. Wrapping prevents calling code conflating this with unrelated errors.
  27. """
  28. class ObjectTypeError(TypeError):
  29. """
  30. Raised when `queryset.get()` failed due to an underlying `TypeError`.
  31. Wrapping prevents calling code conflating this with unrelated errors.
  32. """
  33. class Hyperlink(str):
  34. """
  35. A string like object that additionally has an associated name.
  36. We use this for hyperlinked URLs that may render as a named link
  37. in some contexts, or render as a plain URL in others.
  38. """
  39. def __new__(cls, url, obj):
  40. ret = super().__new__(cls, url)
  41. ret.obj = obj
  42. return ret
  43. def __getnewargs__(self):
  44. return (str(self), self.name)
  45. @property
  46. def name(self):
  47. # This ensures that we only called `__str__` lazily,
  48. # as in some cases calling __str__ on a model instances *might*
  49. # involve a database lookup.
  50. return str(self.obj)
  51. is_hyperlink = True
  52. class PKOnlyObject:
  53. """
  54. This is a mock object, used for when we only need the pk of the object
  55. instance, but still want to return an object with a .pk attribute,
  56. in order to keep the same interface as a regular model instance.
  57. """
  58. def __init__(self, pk):
  59. self.pk = pk
  60. def __str__(self):
  61. return "%s" % self.pk
  62. # We assume that 'validators' are intended for the child serializer,
  63. # rather than the parent serializer.
  64. MANY_RELATION_KWARGS = (
  65. 'read_only', 'write_only', 'required', 'default', 'initial', 'source',
  66. 'label', 'help_text', 'style', 'error_messages', 'allow_empty',
  67. 'html_cutoff', 'html_cutoff_text'
  68. )
  69. class RelatedField(Field):
  70. queryset = None
  71. html_cutoff = None
  72. html_cutoff_text = None
  73. def __init__(self, **kwargs):
  74. self.queryset = kwargs.pop('queryset', self.queryset)
  75. cutoff_from_settings = api_settings.HTML_SELECT_CUTOFF
  76. if cutoff_from_settings is not None:
  77. cutoff_from_settings = int(cutoff_from_settings)
  78. self.html_cutoff = kwargs.pop('html_cutoff', cutoff_from_settings)
  79. self.html_cutoff_text = kwargs.pop(
  80. 'html_cutoff_text',
  81. self.html_cutoff_text or _(api_settings.HTML_SELECT_CUTOFF_TEXT)
  82. )
  83. if not method_overridden('get_queryset', RelatedField, self):
  84. assert self.queryset is not None or kwargs.get('read_only'), (
  85. 'Relational field must provide a `queryset` argument, '
  86. 'override `get_queryset`, or set read_only=`True`.'
  87. )
  88. assert not (self.queryset is not None and kwargs.get('read_only')), (
  89. 'Relational fields should not provide a `queryset` argument, '
  90. 'when setting read_only=`True`.'
  91. )
  92. kwargs.pop('many', None)
  93. kwargs.pop('allow_empty', None)
  94. super().__init__(**kwargs)
  95. def __new__(cls, *args, **kwargs):
  96. # We override this method in order to automagically create
  97. # `ManyRelatedField` classes instead when `many=True` is set.
  98. if kwargs.pop('many', False):
  99. return cls.many_init(*args, **kwargs)
  100. return super().__new__(cls, *args, **kwargs)
  101. @classmethod
  102. def many_init(cls, *args, **kwargs):
  103. """
  104. This method handles creating a parent `ManyRelatedField` instance
  105. when the `many=True` keyword argument is passed.
  106. Typically you won't need to override this method.
  107. Note that we're over-cautious in passing most arguments to both parent
  108. and child classes in order to try to cover the general case. If you're
  109. overriding this method you'll probably want something much simpler, eg:
  110. @classmethod
  111. def many_init(cls, *args, **kwargs):
  112. kwargs['child'] = cls()
  113. return CustomManyRelatedField(*args, **kwargs)
  114. """
  115. list_kwargs = {'child_relation': cls(*args, **kwargs)}
  116. for key in kwargs:
  117. if key in MANY_RELATION_KWARGS:
  118. list_kwargs[key] = kwargs[key]
  119. return ManyRelatedField(**list_kwargs)
  120. def run_validation(self, data=empty):
  121. # We force empty strings to None values for relational fields.
  122. if data == '':
  123. data = None
  124. return super().run_validation(data)
  125. def get_queryset(self):
  126. queryset = self.queryset
  127. if isinstance(queryset, (QuerySet, Manager)):
  128. # Ensure queryset is re-evaluated whenever used.
  129. # Note that actually a `Manager` class may also be used as the
  130. # queryset argument. This occurs on ModelSerializer fields,
  131. # as it allows us to generate a more expressive 'repr' output
  132. # for the field.
  133. # Eg: 'MyRelationship(queryset=ExampleModel.objects.all())'
  134. queryset = queryset.all()
  135. return queryset
  136. def use_pk_only_optimization(self):
  137. return False
  138. def get_attribute(self, instance):
  139. if self.use_pk_only_optimization() and self.source_attrs:
  140. # Optimized case, return a mock object only containing the pk attribute.
  141. try:
  142. attribute_instance = get_attribute(instance, self.source_attrs[:-1])
  143. value = attribute_instance.serializable_value(self.source_attrs[-1])
  144. if is_simple_callable(value):
  145. # Handle edge case where the relationship `source` argument
  146. # points to a `get_relationship()` method on the model.
  147. value = value()
  148. # Handle edge case where relationship `source` argument points
  149. # to an instance instead of a pk (e.g., a `@property`).
  150. value = getattr(value, 'pk', value)
  151. return PKOnlyObject(pk=value)
  152. except AttributeError:
  153. pass
  154. # Standard case, return the object instance.
  155. return super().get_attribute(instance)
  156. def get_choices(self, cutoff=None):
  157. queryset = self.get_queryset()
  158. if queryset is None:
  159. # Ensure that field.choices returns something sensible
  160. # even when accessed with a read-only field.
  161. return {}
  162. if cutoff is not None:
  163. queryset = queryset[:cutoff]
  164. return OrderedDict([
  165. (
  166. self.to_representation(item),
  167. self.display_value(item)
  168. )
  169. for item in queryset
  170. ])
  171. @property
  172. def choices(self):
  173. return self.get_choices()
  174. @property
  175. def grouped_choices(self):
  176. return self.choices
  177. def iter_options(self):
  178. return iter_options(
  179. self.get_choices(cutoff=self.html_cutoff),
  180. cutoff=self.html_cutoff,
  181. cutoff_text=self.html_cutoff_text
  182. )
  183. def display_value(self, instance):
  184. return str(instance)
  185. class StringRelatedField(RelatedField):
  186. """
  187. A read only field that represents its targets using their
  188. plain string representation.
  189. """
  190. def __init__(self, **kwargs):
  191. kwargs['read_only'] = True
  192. super().__init__(**kwargs)
  193. def to_representation(self, value):
  194. return str(value)
  195. class PrimaryKeyRelatedField(RelatedField):
  196. default_error_messages = {
  197. 'required': _('This field is required.'),
  198. 'does_not_exist': _('Invalid pk "{pk_value}" - object does not exist.'),
  199. 'incorrect_type': _('Incorrect type. Expected pk value, received {data_type}.'),
  200. }
  201. def __init__(self, **kwargs):
  202. self.pk_field = kwargs.pop('pk_field', None)
  203. super().__init__(**kwargs)
  204. def use_pk_only_optimization(self):
  205. return True
  206. def to_internal_value(self, data):
  207. if self.pk_field is not None:
  208. data = self.pk_field.to_internal_value(data)
  209. queryset = self.get_queryset()
  210. try:
  211. if isinstance(data, bool):
  212. raise TypeError
  213. return queryset.get(pk=data)
  214. except ObjectDoesNotExist:
  215. self.fail('does_not_exist', pk_value=data)
  216. except (TypeError, ValueError):
  217. self.fail('incorrect_type', data_type=type(data).__name__)
  218. def to_representation(self, value):
  219. if self.pk_field is not None:
  220. return self.pk_field.to_representation(value.pk)
  221. return value.pk
  222. class HyperlinkedRelatedField(RelatedField):
  223. lookup_field = 'pk'
  224. view_name = None
  225. default_error_messages = {
  226. 'required': _('This field is required.'),
  227. 'no_match': _('Invalid hyperlink - No URL match.'),
  228. 'incorrect_match': _('Invalid hyperlink - Incorrect URL match.'),
  229. 'does_not_exist': _('Invalid hyperlink - Object does not exist.'),
  230. 'incorrect_type': _('Incorrect type. Expected URL string, received {data_type}.'),
  231. }
  232. def __init__(self, view_name=None, **kwargs):
  233. if view_name is not None:
  234. self.view_name = view_name
  235. assert self.view_name is not None, 'The `view_name` argument is required.'
  236. self.lookup_field = kwargs.pop('lookup_field', self.lookup_field)
  237. self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field)
  238. self.format = kwargs.pop('format', None)
  239. # We include this simply for dependency injection in tests.
  240. # We can't add it as a class attributes or it would expect an
  241. # implicit `self` argument to be passed.
  242. self.reverse = reverse
  243. super().__init__(**kwargs)
  244. def use_pk_only_optimization(self):
  245. return self.lookup_field == 'pk'
  246. def get_object(self, view_name, view_args, view_kwargs):
  247. """
  248. Return the object corresponding to a matched URL.
  249. Takes the matched URL conf arguments, and should return an
  250. object instance, or raise an `ObjectDoesNotExist` exception.
  251. """
  252. lookup_value = view_kwargs[self.lookup_url_kwarg]
  253. lookup_kwargs = {self.lookup_field: lookup_value}
  254. queryset = self.get_queryset()
  255. try:
  256. return queryset.get(**lookup_kwargs)
  257. except ValueError:
  258. exc = ObjectValueError(str(sys.exc_info()[1]))
  259. raise exc.with_traceback(sys.exc_info()[2])
  260. except TypeError:
  261. exc = ObjectTypeError(str(sys.exc_info()[1]))
  262. raise exc.with_traceback(sys.exc_info()[2])
  263. def get_url(self, obj, view_name, request, format):
  264. """
  265. Given an object, return the URL that hyperlinks to the object.
  266. May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
  267. attributes are not configured to correctly match the URL conf.
  268. """
  269. # Unsaved objects will not yet have a valid URL.
  270. if hasattr(obj, 'pk') and obj.pk in (None, ''):
  271. return None
  272. lookup_value = getattr(obj, self.lookup_field)
  273. kwargs = {self.lookup_url_kwarg: lookup_value}
  274. return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
  275. def to_internal_value(self, data):
  276. request = self.context.get('request')
  277. try:
  278. http_prefix = data.startswith(('http:', 'https:'))
  279. except AttributeError:
  280. self.fail('incorrect_type', data_type=type(data).__name__)
  281. if http_prefix:
  282. # If needed convert absolute URLs to relative path
  283. data = parse.urlparse(data).path
  284. prefix = get_script_prefix()
  285. if data.startswith(prefix):
  286. data = '/' + data[len(prefix):]
  287. data = uri_to_iri(parse.unquote(data))
  288. try:
  289. match = resolve(data)
  290. except Resolver404:
  291. self.fail('no_match')
  292. try:
  293. expected_viewname = request.versioning_scheme.get_versioned_viewname(
  294. self.view_name, request
  295. )
  296. except AttributeError:
  297. expected_viewname = self.view_name
  298. if match.view_name != expected_viewname:
  299. self.fail('incorrect_match')
  300. try:
  301. return self.get_object(match.view_name, match.args, match.kwargs)
  302. except (ObjectDoesNotExist, ObjectValueError, ObjectTypeError):
  303. self.fail('does_not_exist')
  304. def to_representation(self, value):
  305. assert 'request' in self.context, (
  306. "`%s` requires the request in the serializer"
  307. " context. Add `context={'request': request}` when instantiating "
  308. "the serializer." % self.__class__.__name__
  309. )
  310. request = self.context['request']
  311. format = self.context.get('format')
  312. # By default use whatever format is given for the current context
  313. # unless the target is a different type to the source.
  314. #
  315. # Eg. Consider a HyperlinkedIdentityField pointing from a json
  316. # representation to an html property of that representation...
  317. #
  318. # '/snippets/1/' should link to '/snippets/1/highlight/'
  319. # ...but...
  320. # '/snippets/1/.json' should link to '/snippets/1/highlight/.html'
  321. if format and self.format and self.format != format:
  322. format = self.format
  323. # Return the hyperlink, or error if incorrectly configured.
  324. try:
  325. url = self.get_url(value, self.view_name, request, format)
  326. except NoReverseMatch:
  327. msg = (
  328. 'Could not resolve URL for hyperlinked relationship using '
  329. 'view name "%s". You may have failed to include the related '
  330. 'model in your API, or incorrectly configured the '
  331. '`lookup_field` attribute on this field.'
  332. )
  333. if value in ('', None):
  334. value_string = {'': 'the empty string', None: 'None'}[value]
  335. msg += (
  336. " WARNING: The value of the field on the model instance "
  337. "was %s, which may be why it didn't match any "
  338. "entries in your URL conf." % value_string
  339. )
  340. raise ImproperlyConfigured(msg % self.view_name)
  341. if url is None:
  342. return None
  343. return Hyperlink(url, value)
  344. class HyperlinkedIdentityField(HyperlinkedRelatedField):
  345. """
  346. A read-only field that represents the identity URL for an object, itself.
  347. This is in contrast to `HyperlinkedRelatedField` which represents the
  348. URL of relationships to other objects.
  349. """
  350. def __init__(self, view_name=None, **kwargs):
  351. assert view_name is not None, 'The `view_name` argument is required.'
  352. kwargs['read_only'] = True
  353. kwargs['source'] = '*'
  354. super().__init__(view_name, **kwargs)
  355. def use_pk_only_optimization(self):
  356. # We have the complete object instance already. We don't need
  357. # to run the 'only get the pk for this relationship' code.
  358. return False
  359. class SlugRelatedField(RelatedField):
  360. """
  361. A read-write field that represents the target of the relationship
  362. by a unique 'slug' attribute.
  363. """
  364. default_error_messages = {
  365. 'does_not_exist': _('Object with {slug_name}={value} does not exist.'),
  366. 'invalid': _('Invalid value.'),
  367. }
  368. def __init__(self, slug_field=None, **kwargs):
  369. assert slug_field is not None, 'The `slug_field` argument is required.'
  370. self.slug_field = slug_field
  371. super().__init__(**kwargs)
  372. def to_internal_value(self, data):
  373. queryset = self.get_queryset()
  374. try:
  375. return queryset.get(**{self.slug_field: data})
  376. except ObjectDoesNotExist:
  377. self.fail('does_not_exist', slug_name=self.slug_field, value=smart_str(data))
  378. except (TypeError, ValueError):
  379. self.fail('invalid')
  380. def to_representation(self, obj):
  381. return getattr(obj, self.slug_field)
  382. class ManyRelatedField(Field):
  383. """
  384. Relationships with `many=True` transparently get coerced into instead being
  385. a ManyRelatedField with a child relationship.
  386. The `ManyRelatedField` class is responsible for handling iterating through
  387. the values and passing each one to the child relationship.
  388. This class is treated as private API.
  389. You shouldn't generally need to be using this class directly yourself,
  390. and should instead simply set 'many=True' on the relationship.
  391. """
  392. initial = []
  393. default_empty_html = []
  394. default_error_messages = {
  395. 'not_a_list': _('Expected a list of items but got type "{input_type}".'),
  396. 'empty': _('This list may not be empty.')
  397. }
  398. html_cutoff = None
  399. html_cutoff_text = None
  400. def __init__(self, child_relation=None, *args, **kwargs):
  401. self.child_relation = child_relation
  402. self.allow_empty = kwargs.pop('allow_empty', True)
  403. cutoff_from_settings = api_settings.HTML_SELECT_CUTOFF
  404. if cutoff_from_settings is not None:
  405. cutoff_from_settings = int(cutoff_from_settings)
  406. self.html_cutoff = kwargs.pop('html_cutoff', cutoff_from_settings)
  407. self.html_cutoff_text = kwargs.pop(
  408. 'html_cutoff_text',
  409. self.html_cutoff_text or _(api_settings.HTML_SELECT_CUTOFF_TEXT)
  410. )
  411. assert child_relation is not None, '`child_relation` is a required argument.'
  412. super().__init__(*args, **kwargs)
  413. self.child_relation.bind(field_name='', parent=self)
  414. def get_value(self, dictionary):
  415. # We override the default field access in order to support
  416. # lists in HTML forms.
  417. if html.is_html_input(dictionary):
  418. # Don't return [] if the update is partial
  419. if self.field_name not in dictionary:
  420. if getattr(self.root, 'partial', False):
  421. return empty
  422. return dictionary.getlist(self.field_name)
  423. return dictionary.get(self.field_name, empty)
  424. def to_internal_value(self, data):
  425. if isinstance(data, str) or not hasattr(data, '__iter__'):
  426. self.fail('not_a_list', input_type=type(data).__name__)
  427. if not self.allow_empty and len(data) == 0:
  428. self.fail('empty')
  429. return [
  430. self.child_relation.to_internal_value(item)
  431. for item in data
  432. ]
  433. def get_attribute(self, instance):
  434. # Can't have any relationships if not created
  435. if hasattr(instance, 'pk') and instance.pk is None:
  436. return []
  437. relationship = get_attribute(instance, self.source_attrs)
  438. return relationship.all() if hasattr(relationship, 'all') else relationship
  439. def to_representation(self, iterable):
  440. return [
  441. self.child_relation.to_representation(value)
  442. for value in iterable
  443. ]
  444. def get_choices(self, cutoff=None):
  445. return self.child_relation.get_choices(cutoff)
  446. @property
  447. def choices(self):
  448. return self.get_choices()
  449. @property
  450. def grouped_choices(self):
  451. return self.choices
  452. def iter_options(self):
  453. return iter_options(
  454. self.get_choices(cutoff=self.html_cutoff),
  455. cutoff=self.html_cutoff,
  456. cutoff_text=self.html_cutoff_text
  457. )