related.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. import functools
  2. import inspect
  3. from functools import partial
  4. from django import forms
  5. from django.apps import apps
  6. from django.conf import SettingsReference
  7. from django.core import checks, exceptions
  8. from django.db import connection, router
  9. from django.db.backends import utils
  10. from django.db.models import Q
  11. from django.db.models.constants import LOOKUP_SEP
  12. from django.db.models.deletion import CASCADE, SET_DEFAULT, SET_NULL
  13. from django.db.models.query_utils import PathInfo
  14. from django.db.models.utils import make_model_tuple
  15. from django.utils.functional import cached_property
  16. from django.utils.translation import gettext_lazy as _
  17. from . import Field
  18. from .mixins import FieldCacheMixin
  19. from .related_descriptors import (
  20. ForeignKeyDeferredAttribute, ForwardManyToOneDescriptor,
  21. ForwardOneToOneDescriptor, ManyToManyDescriptor,
  22. ReverseManyToOneDescriptor, ReverseOneToOneDescriptor,
  23. )
  24. from .related_lookups import (
  25. RelatedExact, RelatedGreaterThan, RelatedGreaterThanOrEqual, RelatedIn,
  26. RelatedIsNull, RelatedLessThan, RelatedLessThanOrEqual,
  27. )
  28. from .reverse_related import (
  29. ForeignObjectRel, ManyToManyRel, ManyToOneRel, OneToOneRel,
  30. )
  31. RECURSIVE_RELATIONSHIP_CONSTANT = 'self'
  32. def resolve_relation(scope_model, relation):
  33. """
  34. Transform relation into a model or fully-qualified model string of the form
  35. "app_label.ModelName", relative to scope_model.
  36. The relation argument can be:
  37. * RECURSIVE_RELATIONSHIP_CONSTANT, i.e. the string "self", in which case
  38. the model argument will be returned.
  39. * A bare model name without an app_label, in which case scope_model's
  40. app_label will be prepended.
  41. * An "app_label.ModelName" string.
  42. * A model class, which will be returned unchanged.
  43. """
  44. # Check for recursive relations
  45. if relation == RECURSIVE_RELATIONSHIP_CONSTANT:
  46. relation = scope_model
  47. # Look for an "app.Model" relation
  48. if isinstance(relation, str):
  49. if "." not in relation:
  50. relation = "%s.%s" % (scope_model._meta.app_label, relation)
  51. return relation
  52. def lazy_related_operation(function, model, *related_models, **kwargs):
  53. """
  54. Schedule `function` to be called once `model` and all `related_models`
  55. have been imported and registered with the app registry. `function` will
  56. be called with the newly-loaded model classes as its positional arguments,
  57. plus any optional keyword arguments.
  58. The `model` argument must be a model class. Each subsequent positional
  59. argument is another model, or a reference to another model - see
  60. `resolve_relation()` for the various forms these may take. Any relative
  61. references will be resolved relative to `model`.
  62. This is a convenience wrapper for `Apps.lazy_model_operation` - the app
  63. registry model used is the one found in `model._meta.apps`.
  64. """
  65. models = [model] + [resolve_relation(model, rel) for rel in related_models]
  66. model_keys = (make_model_tuple(m) for m in models)
  67. apps = model._meta.apps
  68. return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  69. class RelatedField(FieldCacheMixin, Field):
  70. """Base class that all relational fields inherit from."""
  71. # Field flags
  72. one_to_many = False
  73. one_to_one = False
  74. many_to_many = False
  75. many_to_one = False
  76. @cached_property
  77. def related_model(self):
  78. # Can't cache this property until all the models are loaded.
  79. apps.check_models_ready()
  80. return self.remote_field.model
  81. def check(self, **kwargs):
  82. return [
  83. *super().check(**kwargs),
  84. *self._check_related_name_is_valid(),
  85. *self._check_related_query_name_is_valid(),
  86. *self._check_relation_model_exists(),
  87. *self._check_referencing_to_swapped_model(),
  88. *self._check_clashes(),
  89. ]
  90. def _check_related_name_is_valid(self):
  91. import keyword
  92. related_name = self.remote_field.related_name
  93. if related_name is None:
  94. return []
  95. is_valid_id = not keyword.iskeyword(related_name) and related_name.isidentifier()
  96. if not (is_valid_id or related_name.endswith('+')):
  97. return [
  98. checks.Error(
  99. "The name '%s' is invalid related_name for field %s.%s" %
  100. (self.remote_field.related_name, self.model._meta.object_name,
  101. self.name),
  102. hint="Related name must be a valid Python identifier or end with a '+'",
  103. obj=self,
  104. id='fields.E306',
  105. )
  106. ]
  107. return []
  108. def _check_related_query_name_is_valid(self):
  109. if self.remote_field.is_hidden():
  110. return []
  111. rel_query_name = self.related_query_name()
  112. errors = []
  113. if rel_query_name.endswith('_'):
  114. errors.append(
  115. checks.Error(
  116. "Reverse query name '%s' must not end with an underscore."
  117. % (rel_query_name,),
  118. hint=("Add or change a related_name or related_query_name "
  119. "argument for this field."),
  120. obj=self,
  121. id='fields.E308',
  122. )
  123. )
  124. if LOOKUP_SEP in rel_query_name:
  125. errors.append(
  126. checks.Error(
  127. "Reverse query name '%s' must not contain '%s'."
  128. % (rel_query_name, LOOKUP_SEP),
  129. hint=("Add or change a related_name or related_query_name "
  130. "argument for this field."),
  131. obj=self,
  132. id='fields.E309',
  133. )
  134. )
  135. return errors
  136. def _check_relation_model_exists(self):
  137. rel_is_missing = self.remote_field.model not in self.opts.apps.get_models()
  138. rel_is_string = isinstance(self.remote_field.model, str)
  139. model_name = self.remote_field.model if rel_is_string else self.remote_field.model._meta.object_name
  140. if rel_is_missing and (rel_is_string or not self.remote_field.model._meta.swapped):
  141. return [
  142. checks.Error(
  143. "Field defines a relation with model '%s', which is either "
  144. "not installed, or is abstract." % model_name,
  145. obj=self,
  146. id='fields.E300',
  147. )
  148. ]
  149. return []
  150. def _check_referencing_to_swapped_model(self):
  151. if (self.remote_field.model not in self.opts.apps.get_models() and
  152. not isinstance(self.remote_field.model, str) and
  153. self.remote_field.model._meta.swapped):
  154. model = "%s.%s" % (
  155. self.remote_field.model._meta.app_label,
  156. self.remote_field.model._meta.object_name
  157. )
  158. return [
  159. checks.Error(
  160. "Field defines a relation with the model '%s', which has "
  161. "been swapped out." % model,
  162. hint="Update the relation to point at 'settings.%s'." % self.remote_field.model._meta.swappable,
  163. obj=self,
  164. id='fields.E301',
  165. )
  166. ]
  167. return []
  168. def _check_clashes(self):
  169. """Check accessor and reverse query name clashes."""
  170. from django.db.models.base import ModelBase
  171. errors = []
  172. opts = self.model._meta
  173. # `f.remote_field.model` may be a string instead of a model. Skip if model name is
  174. # not resolved.
  175. if not isinstance(self.remote_field.model, ModelBase):
  176. return []
  177. # Consider that we are checking field `Model.foreign` and the models
  178. # are:
  179. #
  180. # class Target(models.Model):
  181. # model = models.IntegerField()
  182. # model_set = models.IntegerField()
  183. #
  184. # class Model(models.Model):
  185. # foreign = models.ForeignKey(Target)
  186. # m2m = models.ManyToManyField(Target)
  187. # rel_opts.object_name == "Target"
  188. rel_opts = self.remote_field.model._meta
  189. # If the field doesn't install a backward relation on the target model
  190. # (so `is_hidden` returns True), then there are no clashes to check
  191. # and we can skip these fields.
  192. rel_is_hidden = self.remote_field.is_hidden()
  193. rel_name = self.remote_field.get_accessor_name() # i. e. "model_set"
  194. rel_query_name = self.related_query_name() # i. e. "model"
  195. field_name = "%s.%s" % (opts.object_name, self.name) # i. e. "Model.field"
  196. # Check clashes between accessor or reverse query name of `field`
  197. # and any other field name -- i.e. accessor for Model.foreign is
  198. # model_set and it clashes with Target.model_set.
  199. potential_clashes = rel_opts.fields + rel_opts.many_to_many
  200. for clash_field in potential_clashes:
  201. clash_name = "%s.%s" % (rel_opts.object_name, clash_field.name) # i.e. "Target.model_set"
  202. if not rel_is_hidden and clash_field.name == rel_name:
  203. errors.append(
  204. checks.Error(
  205. "Reverse accessor for '%s' clashes with field name '%s'." % (field_name, clash_name),
  206. hint=("Rename field '%s', or add/change a related_name "
  207. "argument to the definition for field '%s'.") % (clash_name, field_name),
  208. obj=self,
  209. id='fields.E302',
  210. )
  211. )
  212. if clash_field.name == rel_query_name:
  213. errors.append(
  214. checks.Error(
  215. "Reverse query name for '%s' clashes with field name '%s'." % (field_name, clash_name),
  216. hint=("Rename field '%s', or add/change a related_name "
  217. "argument to the definition for field '%s'.") % (clash_name, field_name),
  218. obj=self,
  219. id='fields.E303',
  220. )
  221. )
  222. # Check clashes between accessors/reverse query names of `field` and
  223. # any other field accessor -- i. e. Model.foreign accessor clashes with
  224. # Model.m2m accessor.
  225. potential_clashes = (r for r in rel_opts.related_objects if r.field is not self)
  226. for clash_field in potential_clashes:
  227. clash_name = "%s.%s" % ( # i. e. "Model.m2m"
  228. clash_field.related_model._meta.object_name,
  229. clash_field.field.name)
  230. if not rel_is_hidden and clash_field.get_accessor_name() == rel_name:
  231. errors.append(
  232. checks.Error(
  233. "Reverse accessor for '%s' clashes with reverse accessor for '%s'." % (field_name, clash_name),
  234. hint=("Add or change a related_name argument "
  235. "to the definition for '%s' or '%s'.") % (field_name, clash_name),
  236. obj=self,
  237. id='fields.E304',
  238. )
  239. )
  240. if clash_field.get_accessor_name() == rel_query_name:
  241. errors.append(
  242. checks.Error(
  243. "Reverse query name for '%s' clashes with reverse query name for '%s'."
  244. % (field_name, clash_name),
  245. hint=("Add or change a related_name argument "
  246. "to the definition for '%s' or '%s'.") % (field_name, clash_name),
  247. obj=self,
  248. id='fields.E305',
  249. )
  250. )
  251. return errors
  252. def db_type(self, connection):
  253. # By default related field will not have a column as it relates to
  254. # columns from another table.
  255. return None
  256. def contribute_to_class(self, cls, name, private_only=False, **kwargs):
  257. super().contribute_to_class(cls, name, private_only=private_only, **kwargs)
  258. self.opts = cls._meta
  259. if not cls._meta.abstract:
  260. if self.remote_field.related_name:
  261. related_name = self.remote_field.related_name
  262. else:
  263. related_name = self.opts.default_related_name
  264. if related_name:
  265. related_name = related_name % {
  266. 'class': cls.__name__.lower(),
  267. 'model_name': cls._meta.model_name.lower(),
  268. 'app_label': cls._meta.app_label.lower()
  269. }
  270. self.remote_field.related_name = related_name
  271. if self.remote_field.related_query_name:
  272. related_query_name = self.remote_field.related_query_name % {
  273. 'class': cls.__name__.lower(),
  274. 'app_label': cls._meta.app_label.lower(),
  275. }
  276. self.remote_field.related_query_name = related_query_name
  277. def resolve_related_class(model, related, field):
  278. field.remote_field.model = related
  279. field.do_related_class(related, model)
  280. lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  281. def deconstruct(self):
  282. name, path, args, kwargs = super().deconstruct()
  283. if self.remote_field.limit_choices_to:
  284. kwargs['limit_choices_to'] = self.remote_field.limit_choices_to
  285. if self.remote_field.related_name is not None:
  286. kwargs['related_name'] = self.remote_field.related_name
  287. if self.remote_field.related_query_name is not None:
  288. kwargs['related_query_name'] = self.remote_field.related_query_name
  289. return name, path, args, kwargs
  290. def get_forward_related_filter(self, obj):
  291. """
  292. Return the keyword arguments that when supplied to
  293. self.model.object.filter(), would select all instances related through
  294. this field to the remote obj. This is used to build the querysets
  295. returned by related descriptors. obj is an instance of
  296. self.related_field.model.
  297. """
  298. return {
  299. '%s__%s' % (self.name, rh_field.name): getattr(obj, rh_field.attname)
  300. for _, rh_field in self.related_fields
  301. }
  302. def get_reverse_related_filter(self, obj):
  303. """
  304. Complement to get_forward_related_filter(). Return the keyword
  305. arguments that when passed to self.related_field.model.object.filter()
  306. select all instances of self.related_field.model related through
  307. this field to obj. obj is an instance of self.model.
  308. """
  309. base_filter = {
  310. rh_field.attname: getattr(obj, lh_field.attname)
  311. for lh_field, rh_field in self.related_fields
  312. }
  313. descriptor_filter = self.get_extra_descriptor_filter(obj)
  314. base_q = Q(**base_filter)
  315. if isinstance(descriptor_filter, dict):
  316. return base_q & Q(**descriptor_filter)
  317. elif descriptor_filter:
  318. return base_q & descriptor_filter
  319. return base_q
  320. @property
  321. def swappable_setting(self):
  322. """
  323. Get the setting that this is powered from for swapping, or None
  324. if it's not swapped in / marked with swappable=False.
  325. """
  326. if self.swappable:
  327. # Work out string form of "to"
  328. if isinstance(self.remote_field.model, str):
  329. to_string = self.remote_field.model
  330. else:
  331. to_string = self.remote_field.model._meta.label
  332. return apps.get_swappable_settings_name(to_string)
  333. return None
  334. def set_attributes_from_rel(self):
  335. self.name = (
  336. self.name or
  337. (self.remote_field.model._meta.model_name + '_' + self.remote_field.model._meta.pk.name)
  338. )
  339. if self.verbose_name is None:
  340. self.verbose_name = self.remote_field.model._meta.verbose_name
  341. self.remote_field.set_field_name()
  342. def do_related_class(self, other, cls):
  343. self.set_attributes_from_rel()
  344. self.contribute_to_related_class(other, self.remote_field)
  345. def get_limit_choices_to(self):
  346. """
  347. Return ``limit_choices_to`` for this model field.
  348. If it is a callable, it will be invoked and the result will be
  349. returned.
  350. """
  351. if callable(self.remote_field.limit_choices_to):
  352. return self.remote_field.limit_choices_to()
  353. return self.remote_field.limit_choices_to
  354. def formfield(self, **kwargs):
  355. """
  356. Pass ``limit_choices_to`` to the field being constructed.
  357. Only passes it if there is a type that supports related fields.
  358. This is a similar strategy used to pass the ``queryset`` to the field
  359. being constructed.
  360. """
  361. defaults = {}
  362. if hasattr(self.remote_field, 'get_related_field'):
  363. # If this is a callable, do not invoke it here. Just pass
  364. # it in the defaults for when the form class will later be
  365. # instantiated.
  366. limit_choices_to = self.remote_field.limit_choices_to
  367. defaults.update({
  368. 'limit_choices_to': limit_choices_to,
  369. })
  370. defaults.update(kwargs)
  371. return super().formfield(**defaults)
  372. def related_query_name(self):
  373. """
  374. Define the name that can be used to identify this related object in a
  375. table-spanning query.
  376. """
  377. return self.remote_field.related_query_name or self.remote_field.related_name or self.opts.model_name
  378. @property
  379. def target_field(self):
  380. """
  381. When filtering against this relation, return the field on the remote
  382. model against which the filtering should happen.
  383. """
  384. target_fields = self.get_path_info()[-1].target_fields
  385. if len(target_fields) > 1:
  386. raise exceptions.FieldError(
  387. "The relation has multiple target fields, but only single target field was asked for")
  388. return target_fields[0]
  389. def get_cache_name(self):
  390. return self.name
  391. class ForeignObject(RelatedField):
  392. """
  393. Abstraction of the ForeignKey relation to support multi-column relations.
  394. """
  395. # Field flags
  396. many_to_many = False
  397. many_to_one = True
  398. one_to_many = False
  399. one_to_one = False
  400. requires_unique_target = True
  401. related_accessor_class = ReverseManyToOneDescriptor
  402. forward_related_accessor_class = ForwardManyToOneDescriptor
  403. rel_class = ForeignObjectRel
  404. def __init__(self, to, on_delete, from_fields, to_fields, rel=None, related_name=None,
  405. related_query_name=None, limit_choices_to=None, parent_link=False,
  406. swappable=True, **kwargs):
  407. if rel is None:
  408. rel = self.rel_class(
  409. self, to,
  410. related_name=related_name,
  411. related_query_name=related_query_name,
  412. limit_choices_to=limit_choices_to,
  413. parent_link=parent_link,
  414. on_delete=on_delete,
  415. )
  416. super().__init__(rel=rel, **kwargs)
  417. self.from_fields = from_fields
  418. self.to_fields = to_fields
  419. self.swappable = swappable
  420. def check(self, **kwargs):
  421. return [
  422. *super().check(**kwargs),
  423. *self._check_to_fields_exist(),
  424. *self._check_unique_target(),
  425. ]
  426. def _check_to_fields_exist(self):
  427. # Skip nonexistent models.
  428. if isinstance(self.remote_field.model, str):
  429. return []
  430. errors = []
  431. for to_field in self.to_fields:
  432. if to_field:
  433. try:
  434. self.remote_field.model._meta.get_field(to_field)
  435. except exceptions.FieldDoesNotExist:
  436. errors.append(
  437. checks.Error(
  438. "The to_field '%s' doesn't exist on the related "
  439. "model '%s'."
  440. % (to_field, self.remote_field.model._meta.label),
  441. obj=self,
  442. id='fields.E312',
  443. )
  444. )
  445. return errors
  446. def _check_unique_target(self):
  447. rel_is_string = isinstance(self.remote_field.model, str)
  448. if rel_is_string or not self.requires_unique_target:
  449. return []
  450. try:
  451. self.foreign_related_fields
  452. except exceptions.FieldDoesNotExist:
  453. return []
  454. if not self.foreign_related_fields:
  455. return []
  456. unique_foreign_fields = {
  457. frozenset([f.name])
  458. for f in self.remote_field.model._meta.get_fields()
  459. if getattr(f, 'unique', False)
  460. }
  461. unique_foreign_fields.update({
  462. frozenset(ut)
  463. for ut in self.remote_field.model._meta.unique_together
  464. })
  465. foreign_fields = {f.name for f in self.foreign_related_fields}
  466. has_unique_constraint = any(u <= foreign_fields for u in unique_foreign_fields)
  467. if not has_unique_constraint and len(self.foreign_related_fields) > 1:
  468. field_combination = ', '.join(
  469. "'%s'" % rel_field.name for rel_field in self.foreign_related_fields
  470. )
  471. model_name = self.remote_field.model.__name__
  472. return [
  473. checks.Error(
  474. "No subset of the fields %s on model '%s' is unique."
  475. % (field_combination, model_name),
  476. hint=(
  477. "Add unique=True on any of those fields or add at "
  478. "least a subset of them to a unique_together constraint."
  479. ),
  480. obj=self,
  481. id='fields.E310',
  482. )
  483. ]
  484. elif not has_unique_constraint:
  485. field_name = self.foreign_related_fields[0].name
  486. model_name = self.remote_field.model.__name__
  487. return [
  488. checks.Error(
  489. "'%s.%s' must set unique=True because it is referenced by "
  490. "a foreign key." % (model_name, field_name),
  491. obj=self,
  492. id='fields.E311',
  493. )
  494. ]
  495. else:
  496. return []
  497. def deconstruct(self):
  498. name, path, args, kwargs = super().deconstruct()
  499. kwargs['on_delete'] = self.remote_field.on_delete
  500. kwargs['from_fields'] = self.from_fields
  501. kwargs['to_fields'] = self.to_fields
  502. if self.remote_field.parent_link:
  503. kwargs['parent_link'] = self.remote_field.parent_link
  504. # Work out string form of "to"
  505. if isinstance(self.remote_field.model, str):
  506. kwargs['to'] = self.remote_field.model
  507. else:
  508. kwargs['to'] = "%s.%s" % (
  509. self.remote_field.model._meta.app_label,
  510. self.remote_field.model._meta.object_name,
  511. )
  512. # If swappable is True, then see if we're actually pointing to the target
  513. # of a swap.
  514. swappable_setting = self.swappable_setting
  515. if swappable_setting is not None:
  516. # If it's already a settings reference, error
  517. if hasattr(kwargs['to'], "setting_name"):
  518. if kwargs['to'].setting_name != swappable_setting:
  519. raise ValueError(
  520. "Cannot deconstruct a ForeignKey pointing to a model "
  521. "that is swapped in place of more than one model (%s and %s)"
  522. % (kwargs['to'].setting_name, swappable_setting)
  523. )
  524. # Set it
  525. kwargs['to'] = SettingsReference(
  526. kwargs['to'],
  527. swappable_setting,
  528. )
  529. return name, path, args, kwargs
  530. def resolve_related_fields(self):
  531. if not self.from_fields or len(self.from_fields) != len(self.to_fields):
  532. raise ValueError('Foreign Object from and to fields must be the same non-zero length')
  533. if isinstance(self.remote_field.model, str):
  534. raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
  535. related_fields = []
  536. for index in range(len(self.from_fields)):
  537. from_field_name = self.from_fields[index]
  538. to_field_name = self.to_fields[index]
  539. from_field = (self if from_field_name == 'self'
  540. else self.opts.get_field(from_field_name))
  541. to_field = (self.remote_field.model._meta.pk if to_field_name is None
  542. else self.remote_field.model._meta.get_field(to_field_name))
  543. related_fields.append((from_field, to_field))
  544. return related_fields
  545. @property
  546. def related_fields(self):
  547. if not hasattr(self, '_related_fields'):
  548. self._related_fields = self.resolve_related_fields()
  549. return self._related_fields
  550. @property
  551. def reverse_related_fields(self):
  552. return [(rhs_field, lhs_field) for lhs_field, rhs_field in self.related_fields]
  553. @property
  554. def local_related_fields(self):
  555. return tuple(lhs_field for lhs_field, rhs_field in self.related_fields)
  556. @property
  557. def foreign_related_fields(self):
  558. return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  559. def get_local_related_value(self, instance):
  560. return self.get_instance_value_for_fields(instance, self.local_related_fields)
  561. def get_foreign_related_value(self, instance):
  562. return self.get_instance_value_for_fields(instance, self.foreign_related_fields)
  563. @staticmethod
  564. def get_instance_value_for_fields(instance, fields):
  565. ret = []
  566. opts = instance._meta
  567. for field in fields:
  568. # Gotcha: in some cases (like fixture loading) a model can have
  569. # different values in parent_ptr_id and parent's id. So, use
  570. # instance.pk (that is, parent_ptr_id) when asked for instance.id.
  571. if field.primary_key:
  572. possible_parent_link = opts.get_ancestor_link(field.model)
  573. if (not possible_parent_link or
  574. possible_parent_link.primary_key or
  575. possible_parent_link.model._meta.abstract):
  576. ret.append(instance.pk)
  577. continue
  578. ret.append(getattr(instance, field.attname))
  579. return tuple(ret)
  580. def get_attname_column(self):
  581. attname, column = super().get_attname_column()
  582. return attname, None
  583. def get_joining_columns(self, reverse_join=False):
  584. source = self.reverse_related_fields if reverse_join else self.related_fields
  585. return tuple((lhs_field.column, rhs_field.column) for lhs_field, rhs_field in source)
  586. def get_reverse_joining_columns(self):
  587. return self.get_joining_columns(reverse_join=True)
  588. def get_extra_descriptor_filter(self, instance):
  589. """
  590. Return an extra filter condition for related object fetching when
  591. user does 'instance.fieldname', that is the extra filter is used in
  592. the descriptor of the field.
  593. The filter should be either a dict usable in .filter(**kwargs) call or
  594. a Q-object. The condition will be ANDed together with the relation's
  595. joining columns.
  596. A parallel method is get_extra_restriction() which is used in
  597. JOIN and subquery conditions.
  598. """
  599. return {}
  600. def get_extra_restriction(self, where_class, alias, related_alias):
  601. """
  602. Return a pair condition used for joining and subquery pushdown. The
  603. condition is something that responds to as_sql(compiler, connection)
  604. method.
  605. Note that currently referring both the 'alias' and 'related_alias'
  606. will not work in some conditions, like subquery pushdown.
  607. A parallel method is get_extra_descriptor_filter() which is used in
  608. instance.fieldname related object fetching.
  609. """
  610. return None
  611. def get_path_info(self, filtered_relation=None):
  612. """Get path from this field to the related model."""
  613. opts = self.remote_field.model._meta
  614. from_opts = self.model._meta
  615. return [PathInfo(
  616. from_opts=from_opts,
  617. to_opts=opts,
  618. target_fields=self.foreign_related_fields,
  619. join_field=self,
  620. m2m=False,
  621. direct=True,
  622. filtered_relation=filtered_relation,
  623. )]
  624. def get_reverse_path_info(self, filtered_relation=None):
  625. """Get path from the related model to this field's model."""
  626. opts = self.model._meta
  627. from_opts = self.remote_field.model._meta
  628. return [PathInfo(
  629. from_opts=from_opts,
  630. to_opts=opts,
  631. target_fields=(opts.pk,),
  632. join_field=self.remote_field,
  633. m2m=not self.unique,
  634. direct=False,
  635. filtered_relation=filtered_relation,
  636. )]
  637. @classmethod
  638. @functools.lru_cache(maxsize=None)
  639. def get_lookups(cls):
  640. bases = inspect.getmro(cls)
  641. bases = bases[:bases.index(ForeignObject) + 1]
  642. class_lookups = [parent.__dict__.get('class_lookups', {}) for parent in bases]
  643. return cls.merge_dicts(class_lookups)
  644. def contribute_to_class(self, cls, name, private_only=False, **kwargs):
  645. super().contribute_to_class(cls, name, private_only=private_only, **kwargs)
  646. setattr(cls, self.name, self.forward_related_accessor_class(self))
  647. def contribute_to_related_class(self, cls, related):
  648. # Internal FK's - i.e., those with a related name ending with '+' -
  649. # and swapped models don't get a related descriptor.
  650. if not self.remote_field.is_hidden() and not related.related_model._meta.swapped:
  651. setattr(cls._meta.concrete_model, related.get_accessor_name(), self.related_accessor_class(related))
  652. # While 'limit_choices_to' might be a callable, simply pass
  653. # it along for later - this is too early because it's still
  654. # model load time.
  655. if self.remote_field.limit_choices_to:
  656. cls._meta.related_fkey_lookups.append(self.remote_field.limit_choices_to)
  657. ForeignObject.register_lookup(RelatedIn)
  658. ForeignObject.register_lookup(RelatedExact)
  659. ForeignObject.register_lookup(RelatedLessThan)
  660. ForeignObject.register_lookup(RelatedGreaterThan)
  661. ForeignObject.register_lookup(RelatedGreaterThanOrEqual)
  662. ForeignObject.register_lookup(RelatedLessThanOrEqual)
  663. ForeignObject.register_lookup(RelatedIsNull)
  664. class ForeignKey(ForeignObject):
  665. """
  666. Provide a many-to-one relation by adding a column to the local model
  667. to hold the remote value.
  668. By default ForeignKey will target the pk of the remote model but this
  669. behavior can be changed by using the ``to_field`` argument.
  670. """
  671. descriptor_class = ForeignKeyDeferredAttribute
  672. # Field flags
  673. many_to_many = False
  674. many_to_one = True
  675. one_to_many = False
  676. one_to_one = False
  677. rel_class = ManyToOneRel
  678. empty_strings_allowed = False
  679. default_error_messages = {
  680. 'invalid': _('%(model)s instance with %(field)s %(value)r does not exist.')
  681. }
  682. description = _("Foreign Key (type determined by related field)")
  683. def __init__(self, to, on_delete, related_name=None, related_query_name=None,
  684. limit_choices_to=None, parent_link=False, to_field=None,
  685. db_constraint=True, **kwargs):
  686. try:
  687. to._meta.model_name
  688. except AttributeError:
  689. assert isinstance(to, str), (
  690. "%s(%r) is invalid. First parameter to ForeignKey must be "
  691. "either a model, a model name, or the string %r" % (
  692. self.__class__.__name__, to,
  693. RECURSIVE_RELATIONSHIP_CONSTANT,
  694. )
  695. )
  696. else:
  697. # For backwards compatibility purposes, we need to *try* and set
  698. # the to_field during FK construction. It won't be guaranteed to
  699. # be correct until contribute_to_class is called. Refs #12190.
  700. to_field = to_field or (to._meta.pk and to._meta.pk.name)
  701. if not callable(on_delete):
  702. raise TypeError('on_delete must be callable.')
  703. kwargs['rel'] = self.rel_class(
  704. self, to, to_field,
  705. related_name=related_name,
  706. related_query_name=related_query_name,
  707. limit_choices_to=limit_choices_to,
  708. parent_link=parent_link,
  709. on_delete=on_delete,
  710. )
  711. kwargs.setdefault('db_index', True)
  712. super().__init__(to, on_delete, from_fields=['self'], to_fields=[to_field], **kwargs)
  713. self.db_constraint = db_constraint
  714. def check(self, **kwargs):
  715. return [
  716. *super().check(**kwargs),
  717. *self._check_on_delete(),
  718. *self._check_unique(),
  719. ]
  720. def _check_on_delete(self):
  721. on_delete = getattr(self.remote_field, 'on_delete', None)
  722. if on_delete == SET_NULL and not self.null:
  723. return [
  724. checks.Error(
  725. 'Field specifies on_delete=SET_NULL, but cannot be null.',
  726. hint='Set null=True argument on the field, or change the on_delete rule.',
  727. obj=self,
  728. id='fields.E320',
  729. )
  730. ]
  731. elif on_delete == SET_DEFAULT and not self.has_default():
  732. return [
  733. checks.Error(
  734. 'Field specifies on_delete=SET_DEFAULT, but has no default value.',
  735. hint='Set a default value, or change the on_delete rule.',
  736. obj=self,
  737. id='fields.E321',
  738. )
  739. ]
  740. else:
  741. return []
  742. def _check_unique(self, **kwargs):
  743. return [
  744. checks.Warning(
  745. 'Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.',
  746. hint='ForeignKey(unique=True) is usually better served by a OneToOneField.',
  747. obj=self,
  748. id='fields.W342',
  749. )
  750. ] if self.unique else []
  751. def deconstruct(self):
  752. name, path, args, kwargs = super().deconstruct()
  753. del kwargs['to_fields']
  754. del kwargs['from_fields']
  755. # Handle the simpler arguments
  756. if self.db_index:
  757. del kwargs['db_index']
  758. else:
  759. kwargs['db_index'] = False
  760. if self.db_constraint is not True:
  761. kwargs['db_constraint'] = self.db_constraint
  762. # Rel needs more work.
  763. to_meta = getattr(self.remote_field.model, "_meta", None)
  764. if self.remote_field.field_name and (
  765. not to_meta or (to_meta.pk and self.remote_field.field_name != to_meta.pk.name)):
  766. kwargs['to_field'] = self.remote_field.field_name
  767. return name, path, args, kwargs
  768. def to_python(self, value):
  769. return self.target_field.to_python(value)
  770. @property
  771. def target_field(self):
  772. return self.foreign_related_fields[0]
  773. def get_reverse_path_info(self, filtered_relation=None):
  774. """Get path from the related model to this field's model."""
  775. opts = self.model._meta
  776. from_opts = self.remote_field.model._meta
  777. return [PathInfo(
  778. from_opts=from_opts,
  779. to_opts=opts,
  780. target_fields=(opts.pk,),
  781. join_field=self.remote_field,
  782. m2m=not self.unique,
  783. direct=False,
  784. filtered_relation=filtered_relation,
  785. )]
  786. def validate(self, value, model_instance):
  787. if self.remote_field.parent_link:
  788. return
  789. super().validate(value, model_instance)
  790. if value is None:
  791. return
  792. using = router.db_for_read(self.remote_field.model, instance=model_instance)
  793. qs = self.remote_field.model._default_manager.using(using).filter(
  794. **{self.remote_field.field_name: value}
  795. )
  796. qs = qs.complex_filter(self.get_limit_choices_to())
  797. if not qs.exists():
  798. raise exceptions.ValidationError(
  799. self.error_messages['invalid'],
  800. code='invalid',
  801. params={
  802. 'model': self.remote_field.model._meta.verbose_name, 'pk': value,
  803. 'field': self.remote_field.field_name, 'value': value,
  804. }, # 'pk' is included for backwards compatibility
  805. )
  806. def get_attname(self):
  807. return '%s_id' % self.name
  808. def get_attname_column(self):
  809. attname = self.get_attname()
  810. column = self.db_column or attname
  811. return attname, column
  812. def get_default(self):
  813. """Return the to_field if the default value is an object."""
  814. field_default = super().get_default()
  815. if isinstance(field_default, self.remote_field.model):
  816. return getattr(field_default, self.target_field.attname)
  817. return field_default
  818. def get_db_prep_save(self, value, connection):
  819. if value is None or (value == '' and
  820. (not self.target_field.empty_strings_allowed or
  821. connection.features.interprets_empty_strings_as_nulls)):
  822. return None
  823. else:
  824. return self.target_field.get_db_prep_save(value, connection=connection)
  825. def get_db_prep_value(self, value, connection, prepared=False):
  826. return self.target_field.get_db_prep_value(value, connection, prepared)
  827. def get_prep_value(self, value):
  828. return self.target_field.get_prep_value(value)
  829. def contribute_to_related_class(self, cls, related):
  830. super().contribute_to_related_class(cls, related)
  831. if self.remote_field.field_name is None:
  832. self.remote_field.field_name = cls._meta.pk.name
  833. def formfield(self, *, using=None, **kwargs):
  834. if isinstance(self.remote_field.model, str):
  835. raise ValueError("Cannot create form field for %r yet, because "
  836. "its related model %r has not been loaded yet" %
  837. (self.name, self.remote_field.model))
  838. return super().formfield(**{
  839. 'form_class': forms.ModelChoiceField,
  840. 'queryset': self.remote_field.model._default_manager.using(using),
  841. 'to_field_name': self.remote_field.field_name,
  842. **kwargs,
  843. })
  844. def db_check(self, connection):
  845. return []
  846. def db_type(self, connection):
  847. return self.target_field.rel_db_type(connection=connection)
  848. def db_parameters(self, connection):
  849. return {"type": self.db_type(connection), "check": self.db_check(connection)}
  850. def convert_empty_strings(self, value, expression, connection):
  851. if (not value) and isinstance(value, str):
  852. return None
  853. return value
  854. def get_db_converters(self, connection):
  855. converters = super().get_db_converters(connection)
  856. if connection.features.interprets_empty_strings_as_nulls:
  857. converters += [self.convert_empty_strings]
  858. return converters
  859. def get_col(self, alias, output_field=None):
  860. if output_field is None:
  861. output_field = self.target_field
  862. while isinstance(output_field, ForeignKey):
  863. output_field = output_field.target_field
  864. if output_field is self:
  865. raise ValueError('Cannot resolve output_field.')
  866. return super().get_col(alias, output_field)
  867. class OneToOneField(ForeignKey):
  868. """
  869. A OneToOneField is essentially the same as a ForeignKey, with the exception
  870. that it always carries a "unique" constraint with it and the reverse
  871. relation always returns the object pointed to (since there will only ever
  872. be one), rather than returning a list.
  873. """
  874. # Field flags
  875. many_to_many = False
  876. many_to_one = False
  877. one_to_many = False
  878. one_to_one = True
  879. related_accessor_class = ReverseOneToOneDescriptor
  880. forward_related_accessor_class = ForwardOneToOneDescriptor
  881. rel_class = OneToOneRel
  882. description = _("One-to-one relationship")
  883. def __init__(self, to, on_delete, to_field=None, **kwargs):
  884. kwargs['unique'] = True
  885. super().__init__(to, on_delete, to_field=to_field, **kwargs)
  886. def deconstruct(self):
  887. name, path, args, kwargs = super().deconstruct()
  888. if "unique" in kwargs:
  889. del kwargs['unique']
  890. return name, path, args, kwargs
  891. def formfield(self, **kwargs):
  892. if self.remote_field.parent_link:
  893. return None
  894. return super().formfield(**kwargs)
  895. def save_form_data(self, instance, data):
  896. if isinstance(data, self.remote_field.model):
  897. setattr(instance, self.name, data)
  898. else:
  899. setattr(instance, self.attname, data)
  900. # Remote field object must be cleared otherwise Model.save()
  901. # will reassign attname using the related object pk.
  902. if data is None:
  903. setattr(instance, self.name, data)
  904. def _check_unique(self, **kwargs):
  905. # Override ForeignKey since check isn't applicable here.
  906. return []
  907. def create_many_to_many_intermediary_model(field, klass):
  908. from django.db import models
  909. def set_managed(model, related, through):
  910. through._meta.managed = model._meta.managed or related._meta.managed
  911. to_model = resolve_relation(klass, field.remote_field.model)
  912. name = '%s_%s' % (klass._meta.object_name, field.name)
  913. lazy_related_operation(set_managed, klass, to_model, name)
  914. to = make_model_tuple(to_model)[1]
  915. from_ = klass._meta.model_name
  916. if to == from_:
  917. to = 'to_%s' % to
  918. from_ = 'from_%s' % from_
  919. meta = type('Meta', (), {
  920. 'db_table': field._get_m2m_db_table(klass._meta),
  921. 'auto_created': klass,
  922. 'app_label': klass._meta.app_label,
  923. 'db_tablespace': klass._meta.db_tablespace,
  924. 'unique_together': (from_, to),
  925. 'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
  926. 'verbose_name_plural': _('%(from)s-%(to)s relationships') % {'from': from_, 'to': to},
  927. 'apps': field.model._meta.apps,
  928. })
  929. # Construct and return the new class.
  930. return type(name, (models.Model,), {
  931. 'Meta': meta,
  932. '__module__': klass.__module__,
  933. from_: models.ForeignKey(
  934. klass,
  935. related_name='%s+' % name,
  936. db_tablespace=field.db_tablespace,
  937. db_constraint=field.remote_field.db_constraint,
  938. on_delete=CASCADE,
  939. ),
  940. to: models.ForeignKey(
  941. to_model,
  942. related_name='%s+' % name,
  943. db_tablespace=field.db_tablespace,
  944. db_constraint=field.remote_field.db_constraint,
  945. on_delete=CASCADE,
  946. )
  947. })
  948. class ManyToManyField(RelatedField):
  949. """
  950. Provide a many-to-many relation by using an intermediary model that
  951. holds two ForeignKey fields pointed at the two sides of the relation.
  952. Unless a ``through`` model was provided, ManyToManyField will use the
  953. create_many_to_many_intermediary_model factory to automatically generate
  954. the intermediary model.
  955. """
  956. # Field flags
  957. many_to_many = True
  958. many_to_one = False
  959. one_to_many = False
  960. one_to_one = False
  961. rel_class = ManyToManyRel
  962. description = _("Many-to-many relationship")
  963. def __init__(self, to, related_name=None, related_query_name=None,
  964. limit_choices_to=None, symmetrical=None, through=None,
  965. through_fields=None, db_constraint=True, db_table=None,
  966. swappable=True, **kwargs):
  967. try:
  968. to._meta
  969. except AttributeError:
  970. assert isinstance(to, str), (
  971. "%s(%r) is invalid. First parameter to ManyToManyField must be "
  972. "either a model, a model name, or the string %r" %
  973. (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
  974. )
  975. if symmetrical is None:
  976. symmetrical = (to == RECURSIVE_RELATIONSHIP_CONSTANT)
  977. if through is not None:
  978. assert db_table is None, (
  979. "Cannot specify a db_table if an intermediary model is used."
  980. )
  981. kwargs['rel'] = self.rel_class(
  982. self, to,
  983. related_name=related_name,
  984. related_query_name=related_query_name,
  985. limit_choices_to=limit_choices_to,
  986. symmetrical=symmetrical,
  987. through=through,
  988. through_fields=through_fields,
  989. db_constraint=db_constraint,
  990. )
  991. self.has_null_arg = 'null' in kwargs
  992. super().__init__(**kwargs)
  993. self.db_table = db_table
  994. self.swappable = swappable
  995. def check(self, **kwargs):
  996. return [
  997. *super().check(**kwargs),
  998. *self._check_unique(**kwargs),
  999. *self._check_relationship_model(**kwargs),
  1000. *self._check_ignored_options(**kwargs),
  1001. *self._check_table_uniqueness(**kwargs),
  1002. ]
  1003. def _check_unique(self, **kwargs):
  1004. if self.unique:
  1005. return [
  1006. checks.Error(
  1007. 'ManyToManyFields cannot be unique.',
  1008. obj=self,
  1009. id='fields.E330',
  1010. )
  1011. ]
  1012. return []
  1013. def _check_ignored_options(self, **kwargs):
  1014. warnings = []
  1015. if self.has_null_arg:
  1016. warnings.append(
  1017. checks.Warning(
  1018. 'null has no effect on ManyToManyField.',
  1019. obj=self,
  1020. id='fields.W340',
  1021. )
  1022. )
  1023. if self._validators:
  1024. warnings.append(
  1025. checks.Warning(
  1026. 'ManyToManyField does not support validators.',
  1027. obj=self,
  1028. id='fields.W341',
  1029. )
  1030. )
  1031. if (self.remote_field.limit_choices_to and self.remote_field.through and
  1032. not self.remote_field.through._meta.auto_created):
  1033. warnings.append(
  1034. checks.Warning(
  1035. 'limit_choices_to has no effect on ManyToManyField '
  1036. 'with a through model.',
  1037. obj=self,
  1038. id='fields.W343',
  1039. )
  1040. )
  1041. return warnings
  1042. def _check_relationship_model(self, from_model=None, **kwargs):
  1043. if hasattr(self.remote_field.through, '_meta'):
  1044. qualified_model_name = "%s.%s" % (
  1045. self.remote_field.through._meta.app_label, self.remote_field.through.__name__)
  1046. else:
  1047. qualified_model_name = self.remote_field.through
  1048. errors = []
  1049. if self.remote_field.through not in self.opts.apps.get_models(include_auto_created=True):
  1050. # The relationship model is not installed.
  1051. errors.append(
  1052. checks.Error(
  1053. "Field specifies a many-to-many relation through model "
  1054. "'%s', which has not been installed." % qualified_model_name,
  1055. obj=self,
  1056. id='fields.E331',
  1057. )
  1058. )
  1059. else:
  1060. assert from_model is not None, (
  1061. "ManyToManyField with intermediate "
  1062. "tables cannot be checked if you don't pass the model "
  1063. "where the field is attached to."
  1064. )
  1065. # Set some useful local variables
  1066. to_model = resolve_relation(from_model, self.remote_field.model)
  1067. from_model_name = from_model._meta.object_name
  1068. if isinstance(to_model, str):
  1069. to_model_name = to_model
  1070. else:
  1071. to_model_name = to_model._meta.object_name
  1072. relationship_model_name = self.remote_field.through._meta.object_name
  1073. self_referential = from_model == to_model
  1074. # Count foreign keys in intermediate model
  1075. if self_referential:
  1076. seen_self = sum(
  1077. from_model == getattr(field.remote_field, 'model', None)
  1078. for field in self.remote_field.through._meta.fields
  1079. )
  1080. if seen_self > 2 and not self.remote_field.through_fields:
  1081. errors.append(
  1082. checks.Error(
  1083. "The model is used as an intermediate model by "
  1084. "'%s', but it has more than two foreign keys "
  1085. "to '%s', which is ambiguous. You must specify "
  1086. "which two foreign keys Django should use via the "
  1087. "through_fields keyword argument." % (self, from_model_name),
  1088. hint="Use through_fields to specify which two foreign keys Django should use.",
  1089. obj=self.remote_field.through,
  1090. id='fields.E333',
  1091. )
  1092. )
  1093. else:
  1094. # Count foreign keys in relationship model
  1095. seen_from = sum(
  1096. from_model == getattr(field.remote_field, 'model', None)
  1097. for field in self.remote_field.through._meta.fields
  1098. )
  1099. seen_to = sum(
  1100. to_model == getattr(field.remote_field, 'model', None)
  1101. for field in self.remote_field.through._meta.fields
  1102. )
  1103. if seen_from > 1 and not self.remote_field.through_fields:
  1104. errors.append(
  1105. checks.Error(
  1106. ("The model is used as an intermediate model by "
  1107. "'%s', but it has more than one foreign key "
  1108. "from '%s', which is ambiguous. You must specify "
  1109. "which foreign key Django should use via the "
  1110. "through_fields keyword argument.") % (self, from_model_name),
  1111. hint=(
  1112. 'If you want to create a recursive relationship, '
  1113. 'use ForeignKey("self", symmetrical=False, through="%s").'
  1114. ) % relationship_model_name,
  1115. obj=self,
  1116. id='fields.E334',
  1117. )
  1118. )
  1119. if seen_to > 1 and not self.remote_field.through_fields:
  1120. errors.append(
  1121. checks.Error(
  1122. "The model is used as an intermediate model by "
  1123. "'%s', but it has more than one foreign key "
  1124. "to '%s', which is ambiguous. You must specify "
  1125. "which foreign key Django should use via the "
  1126. "through_fields keyword argument." % (self, to_model_name),
  1127. hint=(
  1128. 'If you want to create a recursive relationship, '
  1129. 'use ForeignKey("self", symmetrical=False, through="%s").'
  1130. ) % relationship_model_name,
  1131. obj=self,
  1132. id='fields.E335',
  1133. )
  1134. )
  1135. if seen_from == 0 or seen_to == 0:
  1136. errors.append(
  1137. checks.Error(
  1138. "The model is used as an intermediate model by "
  1139. "'%s', but it does not have a foreign key to '%s' or '%s'." % (
  1140. self, from_model_name, to_model_name
  1141. ),
  1142. obj=self.remote_field.through,
  1143. id='fields.E336',
  1144. )
  1145. )
  1146. # Validate `through_fields`.
  1147. if self.remote_field.through_fields is not None:
  1148. # Validate that we're given an iterable of at least two items
  1149. # and that none of them is "falsy".
  1150. if not (len(self.remote_field.through_fields) >= 2 and
  1151. self.remote_field.through_fields[0] and self.remote_field.through_fields[1]):
  1152. errors.append(
  1153. checks.Error(
  1154. "Field specifies 'through_fields' but does not provide "
  1155. "the names of the two link fields that should be used "
  1156. "for the relation through model '%s'." % qualified_model_name,
  1157. hint="Make sure you specify 'through_fields' as through_fields=('field1', 'field2')",
  1158. obj=self,
  1159. id='fields.E337',
  1160. )
  1161. )
  1162. # Validate the given through fields -- they should be actual
  1163. # fields on the through model, and also be foreign keys to the
  1164. # expected models.
  1165. else:
  1166. assert from_model is not None, (
  1167. "ManyToManyField with intermediate "
  1168. "tables cannot be checked if you don't pass the model "
  1169. "where the field is attached to."
  1170. )
  1171. source, through, target = from_model, self.remote_field.through, self.remote_field.model
  1172. source_field_name, target_field_name = self.remote_field.through_fields[:2]
  1173. for field_name, related_model in ((source_field_name, source),
  1174. (target_field_name, target)):
  1175. possible_field_names = []
  1176. for f in through._meta.fields:
  1177. if hasattr(f, 'remote_field') and getattr(f.remote_field, 'model', None) == related_model:
  1178. possible_field_names.append(f.name)
  1179. if possible_field_names:
  1180. hint = "Did you mean one of the following foreign keys to '%s': %s?" % (
  1181. related_model._meta.object_name,
  1182. ', '.join(possible_field_names),
  1183. )
  1184. else:
  1185. hint = None
  1186. try:
  1187. field = through._meta.get_field(field_name)
  1188. except exceptions.FieldDoesNotExist:
  1189. errors.append(
  1190. checks.Error(
  1191. "The intermediary model '%s' has no field '%s'."
  1192. % (qualified_model_name, field_name),
  1193. hint=hint,
  1194. obj=self,
  1195. id='fields.E338',
  1196. )
  1197. )
  1198. else:
  1199. if not (hasattr(field, 'remote_field') and
  1200. getattr(field.remote_field, 'model', None) == related_model):
  1201. errors.append(
  1202. checks.Error(
  1203. "'%s.%s' is not a foreign key to '%s'." % (
  1204. through._meta.object_name, field_name,
  1205. related_model._meta.object_name,
  1206. ),
  1207. hint=hint,
  1208. obj=self,
  1209. id='fields.E339',
  1210. )
  1211. )
  1212. return errors
  1213. def _check_table_uniqueness(self, **kwargs):
  1214. if isinstance(self.remote_field.through, str) or not self.remote_field.through._meta.managed:
  1215. return []
  1216. registered_tables = {
  1217. model._meta.db_table: model
  1218. for model in self.opts.apps.get_models(include_auto_created=True)
  1219. if model != self.remote_field.through and model._meta.managed
  1220. }
  1221. m2m_db_table = self.m2m_db_table()
  1222. model = registered_tables.get(m2m_db_table)
  1223. # The second condition allows multiple m2m relations on a model if
  1224. # some point to a through model that proxies another through model.
  1225. if model and model._meta.concrete_model != self.remote_field.through._meta.concrete_model:
  1226. if model._meta.auto_created:
  1227. def _get_field_name(model):
  1228. for field in model._meta.auto_created._meta.many_to_many:
  1229. if field.remote_field.through is model:
  1230. return field.name
  1231. opts = model._meta.auto_created._meta
  1232. clashing_obj = '%s.%s' % (opts.label, _get_field_name(model))
  1233. else:
  1234. clashing_obj = model._meta.label
  1235. return [
  1236. checks.Error(
  1237. "The field's intermediary table '%s' clashes with the "
  1238. "table name of '%s'." % (m2m_db_table, clashing_obj),
  1239. obj=self,
  1240. id='fields.E340',
  1241. )
  1242. ]
  1243. return []
  1244. def deconstruct(self):
  1245. name, path, args, kwargs = super().deconstruct()
  1246. # Handle the simpler arguments.
  1247. if self.db_table is not None:
  1248. kwargs['db_table'] = self.db_table
  1249. if self.remote_field.db_constraint is not True:
  1250. kwargs['db_constraint'] = self.remote_field.db_constraint
  1251. # Rel needs more work.
  1252. if isinstance(self.remote_field.model, str):
  1253. kwargs['to'] = self.remote_field.model
  1254. else:
  1255. kwargs['to'] = "%s.%s" % (
  1256. self.remote_field.model._meta.app_label,
  1257. self.remote_field.model._meta.object_name,
  1258. )
  1259. if getattr(self.remote_field, 'through', None) is not None:
  1260. if isinstance(self.remote_field.through, str):
  1261. kwargs['through'] = self.remote_field.through
  1262. elif not self.remote_field.through._meta.auto_created:
  1263. kwargs['through'] = "%s.%s" % (
  1264. self.remote_field.through._meta.app_label,
  1265. self.remote_field.through._meta.object_name,
  1266. )
  1267. # If swappable is True, then see if we're actually pointing to the target
  1268. # of a swap.
  1269. swappable_setting = self.swappable_setting
  1270. if swappable_setting is not None:
  1271. # If it's already a settings reference, error.
  1272. if hasattr(kwargs['to'], "setting_name"):
  1273. if kwargs['to'].setting_name != swappable_setting:
  1274. raise ValueError(
  1275. "Cannot deconstruct a ManyToManyField pointing to a "
  1276. "model that is swapped in place of more than one model "
  1277. "(%s and %s)" % (kwargs['to'].setting_name, swappable_setting)
  1278. )
  1279. kwargs['to'] = SettingsReference(
  1280. kwargs['to'],
  1281. swappable_setting,
  1282. )
  1283. return name, path, args, kwargs
  1284. def _get_path_info(self, direct=False, filtered_relation=None):
  1285. """Called by both direct and indirect m2m traversal."""
  1286. int_model = self.remote_field.through
  1287. linkfield1 = int_model._meta.get_field(self.m2m_field_name())
  1288. linkfield2 = int_model._meta.get_field(self.m2m_reverse_field_name())
  1289. if direct:
  1290. join1infos = linkfield1.get_reverse_path_info()
  1291. join2infos = linkfield2.get_path_info(filtered_relation)
  1292. else:
  1293. join1infos = linkfield2.get_reverse_path_info()
  1294. join2infos = linkfield1.get_path_info(filtered_relation)
  1295. # Get join infos between the last model of join 1 and the first model
  1296. # of join 2. Assume the only reason these may differ is due to model
  1297. # inheritance.
  1298. join1_final = join1infos[-1].to_opts
  1299. join2_initial = join2infos[0].from_opts
  1300. if join1_final is join2_initial:
  1301. intermediate_infos = []
  1302. elif issubclass(join1_final.model, join2_initial.model):
  1303. intermediate_infos = join1_final.get_path_to_parent(join2_initial.model)
  1304. else:
  1305. intermediate_infos = join2_initial.get_path_from_parent(join1_final.model)
  1306. return [*join1infos, *intermediate_infos, *join2infos]
  1307. def get_path_info(self, filtered_relation=None):
  1308. return self._get_path_info(direct=True, filtered_relation=filtered_relation)
  1309. def get_reverse_path_info(self, filtered_relation=None):
  1310. return self._get_path_info(direct=False, filtered_relation=filtered_relation)
  1311. def _get_m2m_db_table(self, opts):
  1312. """
  1313. Function that can be curried to provide the m2m table name for this
  1314. relation.
  1315. """
  1316. if self.remote_field.through is not None:
  1317. return self.remote_field.through._meta.db_table
  1318. elif self.db_table:
  1319. return self.db_table
  1320. else:
  1321. m2m_table_name = '%s_%s' % (utils.strip_quotes(opts.db_table), self.name)
  1322. return utils.truncate_name(m2m_table_name, connection.ops.max_name_length())
  1323. def _get_m2m_attr(self, related, attr):
  1324. """
  1325. Function that can be curried to provide the source accessor or DB
  1326. column name for the m2m table.
  1327. """
  1328. cache_attr = '_m2m_%s_cache' % attr
  1329. if hasattr(self, cache_attr):
  1330. return getattr(self, cache_attr)
  1331. if self.remote_field.through_fields is not None:
  1332. link_field_name = self.remote_field.through_fields[0]
  1333. else:
  1334. link_field_name = None
  1335. for f in self.remote_field.through._meta.fields:
  1336. if (f.is_relation and f.remote_field.model == related.related_model and
  1337. (link_field_name is None or link_field_name == f.name)):
  1338. setattr(self, cache_attr, getattr(f, attr))
  1339. return getattr(self, cache_attr)
  1340. def _get_m2m_reverse_attr(self, related, attr):
  1341. """
  1342. Function that can be curried to provide the related accessor or DB
  1343. column name for the m2m table.
  1344. """
  1345. cache_attr = '_m2m_reverse_%s_cache' % attr
  1346. if hasattr(self, cache_attr):
  1347. return getattr(self, cache_attr)
  1348. found = False
  1349. if self.remote_field.through_fields is not None:
  1350. link_field_name = self.remote_field.through_fields[1]
  1351. else:
  1352. link_field_name = None
  1353. for f in self.remote_field.through._meta.fields:
  1354. if f.is_relation and f.remote_field.model == related.model:
  1355. if link_field_name is None and related.related_model == related.model:
  1356. # If this is an m2m-intermediate to self,
  1357. # the first foreign key you find will be
  1358. # the source column. Keep searching for
  1359. # the second foreign key.
  1360. if found:
  1361. setattr(self, cache_attr, getattr(f, attr))
  1362. break
  1363. else:
  1364. found = True
  1365. elif link_field_name is None or link_field_name == f.name:
  1366. setattr(self, cache_attr, getattr(f, attr))
  1367. break
  1368. return getattr(self, cache_attr)
  1369. def contribute_to_class(self, cls, name, **kwargs):
  1370. # To support multiple relations to self, it's useful to have a non-None
  1371. # related name on symmetrical relations for internal reasons. The
  1372. # concept doesn't make a lot of sense externally ("you want me to
  1373. # specify *what* on my non-reversible relation?!"), so we set it up
  1374. # automatically. The funky name reduces the chance of an accidental
  1375. # clash.
  1376. if self.remote_field.symmetrical and (
  1377. self.remote_field.model == "self" or self.remote_field.model == cls._meta.object_name):
  1378. self.remote_field.related_name = "%s_rel_+" % name
  1379. elif self.remote_field.is_hidden():
  1380. # If the backwards relation is disabled, replace the original
  1381. # related_name with one generated from the m2m field name. Django
  1382. # still uses backwards relations internally and we need to avoid
  1383. # clashes between multiple m2m fields with related_name == '+'.
  1384. self.remote_field.related_name = "_%s_%s_+" % (cls.__name__.lower(), name)
  1385. super().contribute_to_class(cls, name, **kwargs)
  1386. # The intermediate m2m model is not auto created if:
  1387. # 1) There is a manually specified intermediate, or
  1388. # 2) The class owning the m2m field is abstract.
  1389. # 3) The class owning the m2m field has been swapped out.
  1390. if not cls._meta.abstract:
  1391. if self.remote_field.through:
  1392. def resolve_through_model(_, model, field):
  1393. field.remote_field.through = model
  1394. lazy_related_operation(resolve_through_model, cls, self.remote_field.through, field=self)
  1395. elif not cls._meta.swapped:
  1396. self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
  1397. # Add the descriptor for the m2m relation.
  1398. setattr(cls, self.name, ManyToManyDescriptor(self.remote_field, reverse=False))
  1399. # Set up the accessor for the m2m table name for the relation.
  1400. self.m2m_db_table = partial(self._get_m2m_db_table, cls._meta)
  1401. def contribute_to_related_class(self, cls, related):
  1402. # Internal M2Ms (i.e., those with a related name ending with '+')
  1403. # and swapped models don't get a related descriptor.
  1404. if not self.remote_field.is_hidden() and not related.related_model._meta.swapped:
  1405. setattr(cls, related.get_accessor_name(), ManyToManyDescriptor(self.remote_field, reverse=True))
  1406. # Set up the accessors for the column names on the m2m table.
  1407. self.m2m_column_name = partial(self._get_m2m_attr, related, 'column')
  1408. self.m2m_reverse_name = partial(self._get_m2m_reverse_attr, related, 'column')
  1409. self.m2m_field_name = partial(self._get_m2m_attr, related, 'name')
  1410. self.m2m_reverse_field_name = partial(self._get_m2m_reverse_attr, related, 'name')
  1411. get_m2m_rel = partial(self._get_m2m_attr, related, 'remote_field')
  1412. self.m2m_target_field_name = lambda: get_m2m_rel().field_name
  1413. get_m2m_reverse_rel = partial(self._get_m2m_reverse_attr, related, 'remote_field')
  1414. self.m2m_reverse_target_field_name = lambda: get_m2m_reverse_rel().field_name
  1415. def set_attributes_from_rel(self):
  1416. pass
  1417. def value_from_object(self, obj):
  1418. return [] if obj.pk is None else list(getattr(obj, self.attname).all())
  1419. def save_form_data(self, instance, data):
  1420. getattr(instance, self.attname).set(data)
  1421. def formfield(self, *, using=None, **kwargs):
  1422. defaults = {
  1423. 'form_class': forms.ModelMultipleChoiceField,
  1424. 'queryset': self.remote_field.model._default_manager.using(using),
  1425. **kwargs,
  1426. }
  1427. # If initial is passed in, it's a list of related objects, but the
  1428. # MultipleChoiceField takes a list of IDs.
  1429. if defaults.get('initial') is not None:
  1430. initial = defaults['initial']
  1431. if callable(initial):
  1432. initial = initial()
  1433. defaults['initial'] = [i.pk for i in initial]
  1434. return super().formfield(**defaults)
  1435. def db_check(self, connection):
  1436. return None
  1437. def db_type(self, connection):
  1438. # A ManyToManyField is not represented by a single column,
  1439. # so return None.
  1440. return None
  1441. def db_parameters(self, connection):
  1442. return {"type": None, "check": None}