options.py 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. import bisect
  2. import copy
  3. from collections import defaultdict
  4. from django.apps import apps
  5. from django.conf import settings
  6. from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
  7. from django.core.signals import setting_changed
  8. from django.db import connections
  9. from django.db.models import AutoField, Manager, OrderWrt, UniqueConstraint
  10. from django.db.models.query_utils import PathInfo
  11. from django.utils.datastructures import ImmutableList, OrderedSet
  12. from django.utils.functional import cached_property
  13. from django.utils.module_loading import import_string
  14. from django.utils.text import camel_case_to_spaces, format_lazy
  15. from django.utils.translation import override
  16. PROXY_PARENTS = object()
  17. EMPTY_RELATION_TREE = ()
  18. IMMUTABLE_WARNING = (
  19. "The return type of '%s' should never be mutated. If you want to manipulate this "
  20. "list for your own use, make a copy first."
  21. )
  22. DEFAULT_NAMES = (
  23. "verbose_name",
  24. "verbose_name_plural",
  25. "db_table",
  26. "db_table_comment",
  27. "ordering",
  28. "unique_together",
  29. "permissions",
  30. "get_latest_by",
  31. "order_with_respect_to",
  32. "app_label",
  33. "db_tablespace",
  34. "abstract",
  35. "managed",
  36. "proxy",
  37. "swappable",
  38. "auto_created",
  39. "apps",
  40. "default_permissions",
  41. "select_on_save",
  42. "default_related_name",
  43. "required_db_features",
  44. "required_db_vendor",
  45. "base_manager_name",
  46. "default_manager_name",
  47. "indexes",
  48. "constraints",
  49. )
  50. def normalize_together(option_together):
  51. """
  52. option_together can be either a tuple of tuples, or a single
  53. tuple of two strings. Normalize it to a tuple of tuples, so that
  54. calling code can uniformly expect that.
  55. """
  56. try:
  57. if not option_together:
  58. return ()
  59. if not isinstance(option_together, (tuple, list)):
  60. raise TypeError
  61. first_element = option_together[0]
  62. if not isinstance(first_element, (tuple, list)):
  63. option_together = (option_together,)
  64. # Normalize everything to tuples
  65. return tuple(tuple(ot) for ot in option_together)
  66. except TypeError:
  67. # If the value of option_together isn't valid, return it
  68. # verbatim; this will be picked up by the check framework later.
  69. return option_together
  70. def make_immutable_fields_list(name, data):
  71. return ImmutableList(data, warning=IMMUTABLE_WARNING % name)
  72. class Options:
  73. FORWARD_PROPERTIES = {
  74. "fields",
  75. "many_to_many",
  76. "concrete_fields",
  77. "local_concrete_fields",
  78. "_non_pk_concrete_field_names",
  79. "_reverse_one_to_one_field_names",
  80. "_forward_fields_map",
  81. "managers",
  82. "managers_map",
  83. "base_manager",
  84. "default_manager",
  85. }
  86. REVERSE_PROPERTIES = {"related_objects", "fields_map", "_relation_tree"}
  87. default_apps = apps
  88. def __init__(self, meta, app_label=None):
  89. self._get_fields_cache = {}
  90. self.local_fields = []
  91. self.local_many_to_many = []
  92. self.private_fields = []
  93. self.local_managers = []
  94. self.base_manager_name = None
  95. self.default_manager_name = None
  96. self.model_name = None
  97. self.verbose_name = None
  98. self.verbose_name_plural = None
  99. self.db_table = ""
  100. self.db_table_comment = ""
  101. self.ordering = []
  102. self._ordering_clash = False
  103. self.indexes = []
  104. self.constraints = []
  105. self.unique_together = []
  106. self.select_on_save = False
  107. self.default_permissions = ("add", "change", "delete", "view")
  108. self.permissions = []
  109. self.object_name = None
  110. self.app_label = app_label
  111. self.get_latest_by = None
  112. self.order_with_respect_to = None
  113. self.db_tablespace = settings.DEFAULT_TABLESPACE
  114. self.required_db_features = []
  115. self.required_db_vendor = None
  116. self.meta = meta
  117. self.pk = None
  118. self.auto_field = None
  119. self.abstract = False
  120. self.managed = True
  121. self.proxy = False
  122. # For any class that is a proxy (including automatically created
  123. # classes for deferred object loading), proxy_for_model tells us
  124. # which class this model is proxying. Note that proxy_for_model
  125. # can create a chain of proxy models. For non-proxy models, the
  126. # variable is always None.
  127. self.proxy_for_model = None
  128. # For any non-abstract class, the concrete class is the model
  129. # in the end of the proxy_for_model chain. In particular, for
  130. # concrete models, the concrete_model is always the class itself.
  131. self.concrete_model = None
  132. self.swappable = None
  133. self.parents = {}
  134. self.auto_created = False
  135. # List of all lookups defined in ForeignKey 'limit_choices_to' options
  136. # from *other* models. Needed for some admin checks. Internal use only.
  137. self.related_fkey_lookups = []
  138. # A custom app registry to use, if you're making a separate model set.
  139. self.apps = self.default_apps
  140. self.default_related_name = None
  141. @property
  142. def label(self):
  143. return "%s.%s" % (self.app_label, self.object_name)
  144. @property
  145. def label_lower(self):
  146. return "%s.%s" % (self.app_label, self.model_name)
  147. @property
  148. def app_config(self):
  149. # Don't go through get_app_config to avoid triggering imports.
  150. return self.apps.app_configs.get(self.app_label)
  151. def contribute_to_class(self, cls, name):
  152. from django.db import connection
  153. from django.db.backends.utils import truncate_name
  154. cls._meta = self
  155. self.model = cls
  156. # First, construct the default values for these options.
  157. self.object_name = cls.__name__
  158. self.model_name = self.object_name.lower()
  159. self.verbose_name = camel_case_to_spaces(self.object_name)
  160. # Store the original user-defined values for each option,
  161. # for use when serializing the model definition
  162. self.original_attrs = {}
  163. # Next, apply any overridden values from 'class Meta'.
  164. if self.meta:
  165. meta_attrs = self.meta.__dict__.copy()
  166. for name in self.meta.__dict__:
  167. # Ignore any private attributes that Django doesn't care about.
  168. # NOTE: We can't modify a dictionary's contents while looping
  169. # over it, so we loop over the *original* dictionary instead.
  170. if name.startswith("_"):
  171. del meta_attrs[name]
  172. for attr_name in DEFAULT_NAMES:
  173. if attr_name in meta_attrs:
  174. setattr(self, attr_name, meta_attrs.pop(attr_name))
  175. self.original_attrs[attr_name] = getattr(self, attr_name)
  176. elif hasattr(self.meta, attr_name):
  177. setattr(self, attr_name, getattr(self.meta, attr_name))
  178. self.original_attrs[attr_name] = getattr(self, attr_name)
  179. self.unique_together = normalize_together(self.unique_together)
  180. # App label/class name interpolation for names of constraints and
  181. # indexes.
  182. if not self.abstract:
  183. self.constraints = self._format_names(self.constraints)
  184. self.indexes = self._format_names(self.indexes)
  185. # verbose_name_plural is a special case because it uses a 's'
  186. # by default.
  187. if self.verbose_name_plural is None:
  188. self.verbose_name_plural = format_lazy("{}s", self.verbose_name)
  189. # order_with_respect_and ordering are mutually exclusive.
  190. self._ordering_clash = bool(self.ordering and self.order_with_respect_to)
  191. # Any leftover attributes must be invalid.
  192. if meta_attrs != {}:
  193. raise TypeError(
  194. "'class Meta' got invalid attribute(s): %s" % ",".join(meta_attrs)
  195. )
  196. else:
  197. self.verbose_name_plural = format_lazy("{}s", self.verbose_name)
  198. del self.meta
  199. # If the db_table wasn't provided, use the app_label + model_name.
  200. if not self.db_table:
  201. self.db_table = "%s_%s" % (self.app_label, self.model_name)
  202. self.db_table = truncate_name(
  203. self.db_table, connection.ops.max_name_length()
  204. )
  205. if self.swappable:
  206. setting_changed.connect(self.setting_changed)
  207. def _format_names(self, objs):
  208. """App label/class name interpolation for object names."""
  209. names = {"app_label": self.app_label.lower(), "class": self.model_name}
  210. new_objs = []
  211. for obj in objs:
  212. obj = obj.clone()
  213. obj.name %= names
  214. new_objs.append(obj)
  215. return new_objs
  216. def _get_default_pk_class(self):
  217. pk_class_path = getattr(
  218. self.app_config,
  219. "default_auto_field",
  220. settings.DEFAULT_AUTO_FIELD,
  221. )
  222. if self.app_config and self.app_config._is_default_auto_field_overridden:
  223. app_config_class = type(self.app_config)
  224. source = (
  225. f"{app_config_class.__module__}."
  226. f"{app_config_class.__qualname__}.default_auto_field"
  227. )
  228. else:
  229. source = "DEFAULT_AUTO_FIELD"
  230. if not pk_class_path:
  231. raise ImproperlyConfigured(f"{source} must not be empty.")
  232. try:
  233. pk_class = import_string(pk_class_path)
  234. except ImportError as e:
  235. msg = (
  236. f"{source} refers to the module '{pk_class_path}' that could "
  237. f"not be imported."
  238. )
  239. raise ImproperlyConfigured(msg) from e
  240. if not issubclass(pk_class, AutoField):
  241. raise ValueError(
  242. f"Primary key '{pk_class_path}' referred by {source} must "
  243. f"subclass AutoField."
  244. )
  245. return pk_class
  246. def _prepare(self, model):
  247. if self.order_with_respect_to:
  248. # The app registry will not be ready at this point, so we cannot
  249. # use get_field().
  250. query = self.order_with_respect_to
  251. try:
  252. self.order_with_respect_to = next(
  253. f
  254. for f in self._get_fields(reverse=False)
  255. if f.name == query or f.attname == query
  256. )
  257. except StopIteration:
  258. raise FieldDoesNotExist(
  259. "%s has no field named '%s'" % (self.object_name, query)
  260. )
  261. self.ordering = ("_order",)
  262. if not any(
  263. isinstance(field, OrderWrt) for field in model._meta.local_fields
  264. ):
  265. model.add_to_class("_order", OrderWrt())
  266. else:
  267. self.order_with_respect_to = None
  268. if self.pk is None:
  269. if self.parents:
  270. # Promote the first parent link in lieu of adding yet another
  271. # field.
  272. field = next(iter(self.parents.values()))
  273. # Look for a local field with the same name as the
  274. # first parent link. If a local field has already been
  275. # created, use it instead of promoting the parent
  276. already_created = [
  277. fld for fld in self.local_fields if fld.name == field.name
  278. ]
  279. if already_created:
  280. field = already_created[0]
  281. field.primary_key = True
  282. self.setup_pk(field)
  283. else:
  284. pk_class = self._get_default_pk_class()
  285. auto = pk_class(verbose_name="ID", primary_key=True, auto_created=True)
  286. model.add_to_class("id", auto)
  287. def add_manager(self, manager):
  288. self.local_managers.append(manager)
  289. self._expire_cache()
  290. def add_field(self, field, private=False):
  291. # Insert the given field in the order in which it was created, using
  292. # the "creation_counter" attribute of the field.
  293. # Move many-to-many related fields from self.fields into
  294. # self.many_to_many.
  295. if private:
  296. self.private_fields.append(field)
  297. elif field.is_relation and field.many_to_many:
  298. bisect.insort(self.local_many_to_many, field)
  299. else:
  300. bisect.insort(self.local_fields, field)
  301. self.setup_pk(field)
  302. # If the field being added is a relation to another known field,
  303. # expire the cache on this field and the forward cache on the field
  304. # being referenced, because there will be new relationships in the
  305. # cache. Otherwise, expire the cache of references *to* this field.
  306. # The mechanism for getting at the related model is slightly odd -
  307. # ideally, we'd just ask for field.related_model. However, related_model
  308. # is a cached property, and all the models haven't been loaded yet, so
  309. # we need to make sure we don't cache a string reference.
  310. if (
  311. field.is_relation
  312. and hasattr(field.remote_field, "model")
  313. and field.remote_field.model
  314. ):
  315. try:
  316. field.remote_field.model._meta._expire_cache(forward=False)
  317. except AttributeError:
  318. pass
  319. self._expire_cache()
  320. else:
  321. self._expire_cache(reverse=False)
  322. def setup_pk(self, field):
  323. if not self.pk and field.primary_key:
  324. self.pk = field
  325. field.serialize = False
  326. def setup_proxy(self, target):
  327. """
  328. Do the internal setup so that the current model is a proxy for
  329. "target".
  330. """
  331. self.pk = target._meta.pk
  332. self.proxy_for_model = target
  333. self.db_table = target._meta.db_table
  334. def __repr__(self):
  335. return "<Options for %s>" % self.object_name
  336. def __str__(self):
  337. return self.label_lower
  338. def can_migrate(self, connection):
  339. """
  340. Return True if the model can/should be migrated on the `connection`.
  341. `connection` can be either a real connection or a connection alias.
  342. """
  343. if self.proxy or self.swapped or not self.managed:
  344. return False
  345. if isinstance(connection, str):
  346. connection = connections[connection]
  347. if self.required_db_vendor:
  348. return self.required_db_vendor == connection.vendor
  349. if self.required_db_features:
  350. return all(
  351. getattr(connection.features, feat, False)
  352. for feat in self.required_db_features
  353. )
  354. return True
  355. @cached_property
  356. def verbose_name_raw(self):
  357. """Return the untranslated verbose name."""
  358. if isinstance(self.verbose_name, str):
  359. return self.verbose_name
  360. with override(None):
  361. return str(self.verbose_name)
  362. @cached_property
  363. def swapped(self):
  364. """
  365. Has this model been swapped out for another? If so, return the model
  366. name of the replacement; otherwise, return None.
  367. For historical reasons, model name lookups using get_model() are
  368. case insensitive, so we make sure we are case insensitive here.
  369. """
  370. if self.swappable:
  371. swapped_for = getattr(settings, self.swappable, None)
  372. if swapped_for:
  373. try:
  374. swapped_label, swapped_object = swapped_for.split(".")
  375. except ValueError:
  376. # setting not in the format app_label.model_name
  377. # raising ImproperlyConfigured here causes problems with
  378. # test cleanup code - instead it is raised in get_user_model
  379. # or as part of validation.
  380. return swapped_for
  381. if (
  382. "%s.%s" % (swapped_label, swapped_object.lower())
  383. != self.label_lower
  384. ):
  385. return swapped_for
  386. return None
  387. def setting_changed(self, *, setting, **kwargs):
  388. if setting == self.swappable and "swapped" in self.__dict__:
  389. del self.swapped
  390. @cached_property
  391. def managers(self):
  392. managers = []
  393. seen_managers = set()
  394. bases = (b for b in self.model.mro() if hasattr(b, "_meta"))
  395. for depth, base in enumerate(bases):
  396. for manager in base._meta.local_managers:
  397. if manager.name in seen_managers:
  398. continue
  399. manager = copy.copy(manager)
  400. manager.model = self.model
  401. seen_managers.add(manager.name)
  402. managers.append((depth, manager.creation_counter, manager))
  403. return make_immutable_fields_list(
  404. "managers",
  405. (m[2] for m in sorted(managers)),
  406. )
  407. @cached_property
  408. def managers_map(self):
  409. return {manager.name: manager for manager in self.managers}
  410. @cached_property
  411. def base_manager(self):
  412. base_manager_name = self.base_manager_name
  413. if not base_manager_name:
  414. # Get the first parent's base_manager_name if there's one.
  415. for parent in self.model.mro()[1:]:
  416. if hasattr(parent, "_meta"):
  417. if parent._base_manager.name != "_base_manager":
  418. base_manager_name = parent._base_manager.name
  419. break
  420. if base_manager_name:
  421. try:
  422. return self.managers_map[base_manager_name]
  423. except KeyError:
  424. raise ValueError(
  425. "%s has no manager named %r"
  426. % (
  427. self.object_name,
  428. base_manager_name,
  429. )
  430. )
  431. manager = Manager()
  432. manager.name = "_base_manager"
  433. manager.model = self.model
  434. manager.auto_created = True
  435. return manager
  436. @cached_property
  437. def default_manager(self):
  438. default_manager_name = self.default_manager_name
  439. if not default_manager_name and not self.local_managers:
  440. # Get the first parent's default_manager_name if there's one.
  441. for parent in self.model.mro()[1:]:
  442. if hasattr(parent, "_meta"):
  443. default_manager_name = parent._meta.default_manager_name
  444. break
  445. if default_manager_name:
  446. try:
  447. return self.managers_map[default_manager_name]
  448. except KeyError:
  449. raise ValueError(
  450. "%s has no manager named %r"
  451. % (
  452. self.object_name,
  453. default_manager_name,
  454. )
  455. )
  456. if self.managers:
  457. return self.managers[0]
  458. @cached_property
  459. def fields(self):
  460. """
  461. Return a list of all forward fields on the model and its parents,
  462. excluding ManyToManyFields.
  463. Private API intended only to be used by Django itself; get_fields()
  464. combined with filtering of field properties is the public API for
  465. obtaining this field list.
  466. """
  467. # For legacy reasons, the fields property should only contain forward
  468. # fields that are not private or with a m2m cardinality. Therefore we
  469. # pass these three filters as filters to the generator.
  470. # The third lambda is a longwinded way of checking f.related_model - we don't
  471. # use that property directly because related_model is a cached property,
  472. # and all the models may not have been loaded yet; we don't want to cache
  473. # the string reference to the related_model.
  474. def is_not_an_m2m_field(f):
  475. return not (f.is_relation and f.many_to_many)
  476. def is_not_a_generic_relation(f):
  477. return not (f.is_relation and f.one_to_many)
  478. def is_not_a_generic_foreign_key(f):
  479. return not (
  480. f.is_relation
  481. and f.many_to_one
  482. and not (hasattr(f.remote_field, "model") and f.remote_field.model)
  483. )
  484. return make_immutable_fields_list(
  485. "fields",
  486. (
  487. f
  488. for f in self._get_fields(reverse=False)
  489. if is_not_an_m2m_field(f)
  490. and is_not_a_generic_relation(f)
  491. and is_not_a_generic_foreign_key(f)
  492. ),
  493. )
  494. @cached_property
  495. def concrete_fields(self):
  496. """
  497. Return a list of all concrete fields on the model and its parents.
  498. Private API intended only to be used by Django itself; get_fields()
  499. combined with filtering of field properties is the public API for
  500. obtaining this field list.
  501. """
  502. return make_immutable_fields_list(
  503. "concrete_fields", (f for f in self.fields if f.concrete)
  504. )
  505. @cached_property
  506. def local_concrete_fields(self):
  507. """
  508. Return a list of all concrete fields on the model.
  509. Private API intended only to be used by Django itself; get_fields()
  510. combined with filtering of field properties is the public API for
  511. obtaining this field list.
  512. """
  513. return make_immutable_fields_list(
  514. "local_concrete_fields", (f for f in self.local_fields if f.concrete)
  515. )
  516. @cached_property
  517. def many_to_many(self):
  518. """
  519. Return a list of all many to many fields on the model and its parents.
  520. Private API intended only to be used by Django itself; get_fields()
  521. combined with filtering of field properties is the public API for
  522. obtaining this list.
  523. """
  524. return make_immutable_fields_list(
  525. "many_to_many",
  526. (
  527. f
  528. for f in self._get_fields(reverse=False)
  529. if f.is_relation and f.many_to_many
  530. ),
  531. )
  532. @cached_property
  533. def related_objects(self):
  534. """
  535. Return all related objects pointing to the current model. The related
  536. objects can come from a one-to-one, one-to-many, or many-to-many field
  537. relation type.
  538. Private API intended only to be used by Django itself; get_fields()
  539. combined with filtering of field properties is the public API for
  540. obtaining this field list.
  541. """
  542. all_related_fields = self._get_fields(
  543. forward=False, reverse=True, include_hidden=True
  544. )
  545. return make_immutable_fields_list(
  546. "related_objects",
  547. (
  548. obj
  549. for obj in all_related_fields
  550. if not obj.hidden or obj.field.many_to_many
  551. ),
  552. )
  553. @cached_property
  554. def _forward_fields_map(self):
  555. res = {}
  556. fields = self._get_fields(reverse=False)
  557. for field in fields:
  558. res[field.name] = field
  559. # Due to the way Django's internals work, get_field() should also
  560. # be able to fetch a field by attname. In the case of a concrete
  561. # field with relation, includes the *_id name too
  562. try:
  563. res[field.attname] = field
  564. except AttributeError:
  565. pass
  566. return res
  567. @cached_property
  568. def fields_map(self):
  569. res = {}
  570. fields = self._get_fields(forward=False, include_hidden=True)
  571. for field in fields:
  572. res[field.name] = field
  573. # Due to the way Django's internals work, get_field() should also
  574. # be able to fetch a field by attname. In the case of a concrete
  575. # field with relation, includes the *_id name too
  576. try:
  577. res[field.attname] = field
  578. except AttributeError:
  579. pass
  580. return res
  581. def get_field(self, field_name):
  582. """
  583. Return a field instance given the name of a forward or reverse field.
  584. """
  585. try:
  586. # In order to avoid premature loading of the relation tree
  587. # (expensive) we prefer checking if the field is a forward field.
  588. return self._forward_fields_map[field_name]
  589. except KeyError:
  590. # If the app registry is not ready, reverse fields are
  591. # unavailable, therefore we throw a FieldDoesNotExist exception.
  592. if not self.apps.models_ready:
  593. raise FieldDoesNotExist(
  594. "%s has no field named '%s'. The app cache isn't ready yet, "
  595. "so if this is an auto-created related field, it won't "
  596. "be available yet." % (self.object_name, field_name)
  597. )
  598. try:
  599. # Retrieve field instance by name from cached or just-computed
  600. # field map.
  601. return self.fields_map[field_name]
  602. except KeyError:
  603. raise FieldDoesNotExist(
  604. "%s has no field named '%s'" % (self.object_name, field_name)
  605. )
  606. def get_base_chain(self, model):
  607. """
  608. Return a list of parent classes leading to `model` (ordered from
  609. closest to most distant ancestor). This has to handle the case where
  610. `model` is a grandparent or even more distant relation.
  611. """
  612. if not self.parents:
  613. return []
  614. if model in self.parents:
  615. return [model]
  616. for parent in self.parents:
  617. res = parent._meta.get_base_chain(model)
  618. if res:
  619. res.insert(0, parent)
  620. return res
  621. return []
  622. @cached_property
  623. def all_parents(self):
  624. """
  625. Return all the ancestors of this model as a tuple ordered by MRO.
  626. Useful for determining if something is an ancestor, regardless of lineage.
  627. """
  628. result = OrderedSet(self.parents)
  629. for parent in self.parents:
  630. for ancestor in parent._meta.all_parents:
  631. result.add(ancestor)
  632. return tuple(result)
  633. def get_parent_list(self):
  634. """
  635. Return all the ancestors of this model as a list ordered by MRO.
  636. Backward compatibility method.
  637. """
  638. return list(self.all_parents)
  639. def get_ancestor_link(self, ancestor):
  640. """
  641. Return the field on the current model which points to the given
  642. "ancestor". This is possible an indirect link (a pointer to a parent
  643. model, which points, eventually, to the ancestor). Used when
  644. constructing table joins for model inheritance.
  645. Return None if the model isn't an ancestor of this one.
  646. """
  647. if ancestor in self.parents:
  648. return self.parents[ancestor]
  649. for parent in self.parents:
  650. # Tries to get a link field from the immediate parent
  651. parent_link = parent._meta.get_ancestor_link(ancestor)
  652. if parent_link:
  653. # In case of a proxied model, the first link
  654. # of the chain to the ancestor is that parent
  655. # links
  656. return self.parents[parent] or parent_link
  657. def get_path_to_parent(self, parent):
  658. """
  659. Return a list of PathInfos containing the path from the current
  660. model to the parent model, or an empty list if parent is not a
  661. parent of the current model.
  662. """
  663. if self.model is parent:
  664. return []
  665. # Skip the chain of proxy to the concrete proxied model.
  666. proxied_model = self.concrete_model
  667. path = []
  668. opts = self
  669. for int_model in self.get_base_chain(parent):
  670. if int_model is proxied_model:
  671. opts = int_model._meta
  672. else:
  673. final_field = opts.parents[int_model]
  674. targets = (final_field.remote_field.get_related_field(),)
  675. opts = int_model._meta
  676. path.append(
  677. PathInfo(
  678. from_opts=final_field.model._meta,
  679. to_opts=opts,
  680. target_fields=targets,
  681. join_field=final_field,
  682. m2m=False,
  683. direct=True,
  684. filtered_relation=None,
  685. )
  686. )
  687. return path
  688. def get_path_from_parent(self, parent):
  689. """
  690. Return a list of PathInfos containing the path from the parent
  691. model to the current model, or an empty list if parent is not a
  692. parent of the current model.
  693. """
  694. if self.model is parent:
  695. return []
  696. model = self.concrete_model
  697. # Get a reversed base chain including both the current and parent
  698. # models.
  699. chain = model._meta.get_base_chain(parent)
  700. chain.reverse()
  701. chain.append(model)
  702. # Construct a list of the PathInfos between models in chain.
  703. path = []
  704. for i, ancestor in enumerate(chain[:-1]):
  705. child = chain[i + 1]
  706. link = child._meta.get_ancestor_link(ancestor)
  707. path.extend(link.reverse_path_infos)
  708. return path
  709. def _populate_directed_relation_graph(self):
  710. """
  711. This method is used by each model to find its reverse objects. As this
  712. method is very expensive and is accessed frequently (it looks up every
  713. field in a model, in every app), it is computed on first access and then
  714. is set as a property on every model.
  715. """
  716. related_objects_graph = defaultdict(list)
  717. all_models = self.apps.get_models(include_auto_created=True)
  718. for model in all_models:
  719. opts = model._meta
  720. # Abstract model's fields are copied to child models, hence we will
  721. # see the fields from the child models.
  722. if opts.abstract:
  723. continue
  724. fields_with_relations = (
  725. f
  726. for f in opts._get_fields(reverse=False, include_parents=False)
  727. if f.is_relation and f.related_model is not None
  728. )
  729. for f in fields_with_relations:
  730. if not isinstance(f.remote_field.model, str):
  731. remote_label = f.remote_field.model._meta.concrete_model._meta.label
  732. related_objects_graph[remote_label].append(f)
  733. for model in all_models:
  734. # Set the relation_tree using the internal __dict__. In this way
  735. # we avoid calling the cached property. In attribute lookup,
  736. # __dict__ takes precedence over a data descriptor (such as
  737. # @cached_property). This means that the _meta._relation_tree is
  738. # only called if related_objects is not in __dict__.
  739. related_objects = related_objects_graph[
  740. model._meta.concrete_model._meta.label
  741. ]
  742. model._meta.__dict__["_relation_tree"] = related_objects
  743. # It seems it is possible that self is not in all_models, so guard
  744. # against that with default for get().
  745. return self.__dict__.get("_relation_tree", EMPTY_RELATION_TREE)
  746. @cached_property
  747. def _relation_tree(self):
  748. return self._populate_directed_relation_graph()
  749. def _expire_cache(self, forward=True, reverse=True):
  750. # This method is usually called by apps.cache_clear(), when the
  751. # registry is finalized, or when a new field is added.
  752. if forward:
  753. for cache_key in self.FORWARD_PROPERTIES:
  754. if cache_key in self.__dict__:
  755. delattr(self, cache_key)
  756. if reverse and not self.abstract:
  757. for cache_key in self.REVERSE_PROPERTIES:
  758. if cache_key in self.__dict__:
  759. delattr(self, cache_key)
  760. self._get_fields_cache = {}
  761. def get_fields(self, include_parents=True, include_hidden=False):
  762. """
  763. Return a list of fields associated to the model. By default, include
  764. forward and reverse fields, fields derived from inheritance, but not
  765. hidden fields. The returned fields can be changed using the parameters:
  766. - include_parents: include fields derived from inheritance
  767. - include_hidden: include fields that have a related_name that
  768. starts with a "+"
  769. """
  770. if include_parents is False:
  771. include_parents = PROXY_PARENTS
  772. return self._get_fields(
  773. include_parents=include_parents, include_hidden=include_hidden
  774. )
  775. def _get_fields(
  776. self,
  777. forward=True,
  778. reverse=True,
  779. include_parents=True,
  780. include_hidden=False,
  781. topmost_call=True,
  782. ):
  783. """
  784. Internal helper function to return fields of the model.
  785. * If forward=True, then fields defined on this model are returned.
  786. * If reverse=True, then relations pointing to this model are returned.
  787. * If include_hidden=True, then fields with is_hidden=True are returned.
  788. * The include_parents argument toggles if fields from parent models
  789. should be included. It has three values: True, False, and
  790. PROXY_PARENTS. When set to PROXY_PARENTS, the call will return all
  791. fields defined for the current model or any of its parents in the
  792. parent chain to the model's concrete model.
  793. """
  794. if include_parents not in (True, False, PROXY_PARENTS):
  795. raise TypeError(
  796. "Invalid argument for include_parents: %s" % (include_parents,)
  797. )
  798. # This helper function is used to allow recursion in ``get_fields()``
  799. # implementation and to provide a fast way for Django's internals to
  800. # access specific subsets of fields.
  801. # Creates a cache key composed of all arguments
  802. cache_key = (forward, reverse, include_parents, include_hidden, topmost_call)
  803. try:
  804. # In order to avoid list manipulation. Always return a shallow copy
  805. # of the results.
  806. return self._get_fields_cache[cache_key]
  807. except KeyError:
  808. pass
  809. fields = []
  810. # Recursively call _get_fields() on each parent, with the same
  811. # options provided in this call.
  812. if include_parents is not False:
  813. # In diamond inheritance it is possible that we see the same model
  814. # from two different routes. In that case, avoid adding fields from
  815. # the same parent again.
  816. parent_fields = set()
  817. for parent in self.parents:
  818. if (
  819. parent._meta.concrete_model != self.concrete_model
  820. and include_parents == PROXY_PARENTS
  821. ):
  822. continue
  823. for obj in parent._meta._get_fields(
  824. forward=forward,
  825. reverse=reverse,
  826. include_parents=include_parents,
  827. include_hidden=include_hidden,
  828. topmost_call=False,
  829. ):
  830. if (
  831. not getattr(obj, "parent_link", False)
  832. or obj.model == self.concrete_model
  833. ) and obj not in parent_fields:
  834. fields.append(obj)
  835. parent_fields.add(obj)
  836. if reverse and not self.proxy:
  837. # Tree is computed once and cached until the app cache is expired.
  838. # It is composed of a list of fields pointing to the current model
  839. # from other models.
  840. all_fields = self._relation_tree
  841. for field in all_fields:
  842. # If hidden fields should be included or the relation is not
  843. # intentionally hidden, add to the fields dict.
  844. if include_hidden or not field.remote_field.hidden:
  845. fields.append(field.remote_field)
  846. if forward:
  847. fields += self.local_fields
  848. fields += self.local_many_to_many
  849. # Private fields are recopied to each child model, and they get a
  850. # different model as field.model in each child. Hence we have to
  851. # add the private fields separately from the topmost call. If we
  852. # did this recursively similar to local_fields, we would get field
  853. # instances with field.model != self.model.
  854. if topmost_call:
  855. fields += self.private_fields
  856. # In order to avoid list manipulation. Always
  857. # return a shallow copy of the results
  858. fields = make_immutable_fields_list("get_fields()", fields)
  859. # Store result into cache for later access
  860. self._get_fields_cache[cache_key] = fields
  861. return fields
  862. @cached_property
  863. def total_unique_constraints(self):
  864. """
  865. Return a list of total unique constraints. Useful for determining set
  866. of fields guaranteed to be unique for all rows.
  867. """
  868. return [
  869. constraint
  870. for constraint in self.constraints
  871. if (
  872. isinstance(constraint, UniqueConstraint)
  873. and constraint.condition is None
  874. and not constraint.contains_expressions
  875. )
  876. ]
  877. @cached_property
  878. def _property_names(self):
  879. """Return a set of the names of the properties defined on the model."""
  880. names = set()
  881. seen = set()
  882. for klass in self.model.__mro__:
  883. names |= {
  884. name
  885. for name, value in klass.__dict__.items()
  886. if isinstance(value, property) and name not in seen
  887. }
  888. seen |= set(klass.__dict__)
  889. return frozenset(names)
  890. @cached_property
  891. def _non_pk_concrete_field_names(self):
  892. """
  893. Return a set of the non-pk concrete field names defined on the model.
  894. """
  895. names = []
  896. for field in self.concrete_fields:
  897. if not field.primary_key:
  898. names.append(field.name)
  899. if field.name != field.attname:
  900. names.append(field.attname)
  901. return frozenset(names)
  902. @cached_property
  903. def _reverse_one_to_one_field_names(self):
  904. """
  905. Return a set of reverse one to one field names pointing to the current
  906. model.
  907. """
  908. return frozenset(
  909. field.name for field in self.related_objects if field.one_to_one
  910. )
  911. @cached_property
  912. def db_returning_fields(self):
  913. """
  914. Private API intended only to be used by Django itself.
  915. Fields to be returned after a database insert.
  916. """
  917. return [
  918. field
  919. for field in self._get_fields(
  920. forward=True, reverse=False, include_parents=PROXY_PARENTS
  921. )
  922. if getattr(field, "db_returning", False)
  923. ]