Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

options.py 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. import bisect
  2. import copy
  3. import inspect
  4. from collections import defaultdict
  5. from django.apps import apps
  6. from django.conf import settings
  7. from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
  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. "ordering",
  27. "unique_together",
  28. "permissions",
  29. "get_latest_by",
  30. "order_with_respect_to",
  31. "app_label",
  32. "db_tablespace",
  33. "abstract",
  34. "managed",
  35. "proxy",
  36. "swappable",
  37. "auto_created",
  38. "index_together",
  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. "_forward_fields_map",
  79. "managers",
  80. "managers_map",
  81. "base_manager",
  82. "default_manager",
  83. }
  84. REVERSE_PROPERTIES = {"related_objects", "fields_map", "_relation_tree"}
  85. default_apps = apps
  86. def __init__(self, meta, app_label=None):
  87. self._get_fields_cache = {}
  88. self.local_fields = []
  89. self.local_many_to_many = []
  90. self.private_fields = []
  91. self.local_managers = []
  92. self.base_manager_name = None
  93. self.default_manager_name = None
  94. self.model_name = None
  95. self.verbose_name = None
  96. self.verbose_name_plural = None
  97. self.db_table = ""
  98. self.ordering = []
  99. self._ordering_clash = False
  100. self.indexes = []
  101. self.constraints = []
  102. self.unique_together = []
  103. self.index_together = []
  104. self.select_on_save = False
  105. self.default_permissions = ("add", "change", "delete", "view")
  106. self.permissions = []
  107. self.object_name = None
  108. self.app_label = app_label
  109. self.get_latest_by = None
  110. self.order_with_respect_to = None
  111. self.db_tablespace = settings.DEFAULT_TABLESPACE
  112. self.required_db_features = []
  113. self.required_db_vendor = None
  114. self.meta = meta
  115. self.pk = None
  116. self.auto_field = None
  117. self.abstract = False
  118. self.managed = True
  119. self.proxy = False
  120. # For any class that is a proxy (including automatically created
  121. # classes for deferred object loading), proxy_for_model tells us
  122. # which class this model is proxying. Note that proxy_for_model
  123. # can create a chain of proxy models. For non-proxy models, the
  124. # variable is always None.
  125. self.proxy_for_model = None
  126. # For any non-abstract class, the concrete class is the model
  127. # in the end of the proxy_for_model chain. In particular, for
  128. # concrete models, the concrete_model is always the class itself.
  129. self.concrete_model = None
  130. self.swappable = None
  131. self.parents = {}
  132. self.auto_created = False
  133. # List of all lookups defined in ForeignKey 'limit_choices_to' options
  134. # from *other* models. Needed for some admin checks. Internal use only.
  135. self.related_fkey_lookups = []
  136. # A custom app registry to use, if you're making a separate model set.
  137. self.apps = self.default_apps
  138. self.default_related_name = None
  139. @property
  140. def label(self):
  141. return "%s.%s" % (self.app_label, self.object_name)
  142. @property
  143. def label_lower(self):
  144. return "%s.%s" % (self.app_label, self.model_name)
  145. @property
  146. def app_config(self):
  147. # Don't go through get_app_config to avoid triggering imports.
  148. return self.apps.app_configs.get(self.app_label)
  149. def contribute_to_class(self, cls, name):
  150. from django.db import connection
  151. from django.db.backends.utils import truncate_name
  152. cls._meta = self
  153. self.model = cls
  154. # First, construct the default values for these options.
  155. self.object_name = cls.__name__
  156. self.model_name = self.object_name.lower()
  157. self.verbose_name = camel_case_to_spaces(self.object_name)
  158. # Store the original user-defined values for each option,
  159. # for use when serializing the model definition
  160. self.original_attrs = {}
  161. # Next, apply any overridden values from 'class Meta'.
  162. if self.meta:
  163. meta_attrs = self.meta.__dict__.copy()
  164. for name in self.meta.__dict__:
  165. # Ignore any private attributes that Django doesn't care about.
  166. # NOTE: We can't modify a dictionary's contents while looping
  167. # over it, so we loop over the *original* dictionary instead.
  168. if name.startswith("_"):
  169. del meta_attrs[name]
  170. for attr_name in DEFAULT_NAMES:
  171. if attr_name in meta_attrs:
  172. setattr(self, attr_name, meta_attrs.pop(attr_name))
  173. self.original_attrs[attr_name] = getattr(self, attr_name)
  174. elif hasattr(self.meta, attr_name):
  175. setattr(self, attr_name, getattr(self.meta, attr_name))
  176. self.original_attrs[attr_name] = getattr(self, attr_name)
  177. self.unique_together = normalize_together(self.unique_together)
  178. self.index_together = normalize_together(self.index_together)
  179. # App label/class name interpolation for names of constraints and
  180. # indexes.
  181. if not getattr(cls._meta, "abstract", False):
  182. for attr_name in {"constraints", "indexes"}:
  183. objs = getattr(self, attr_name, [])
  184. setattr(self, attr_name, self._format_names_with_class(cls, objs))
  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. def _format_names_with_class(self, cls, objs):
  206. """App label/class name interpolation for object names."""
  207. new_objs = []
  208. for obj in objs:
  209. obj = obj.clone()
  210. obj.name = obj.name % {
  211. "app_label": cls._meta.app_label.lower(),
  212. "class": cls.__name__.lower(),
  213. }
  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. @property
  356. def verbose_name_raw(self):
  357. """Return the untranslated verbose name."""
  358. with override(None):
  359. return str(self.verbose_name)
  360. @property
  361. def swapped(self):
  362. """
  363. Has this model been swapped out for another? If so, return the model
  364. name of the replacement; otherwise, return None.
  365. For historical reasons, model name lookups using get_model() are
  366. case insensitive, so we make sure we are case insensitive here.
  367. """
  368. if self.swappable:
  369. swapped_for = getattr(settings, self.swappable, None)
  370. if swapped_for:
  371. try:
  372. swapped_label, swapped_object = swapped_for.split(".")
  373. except ValueError:
  374. # setting not in the format app_label.model_name
  375. # raising ImproperlyConfigured here causes problems with
  376. # test cleanup code - instead it is raised in get_user_model
  377. # or as part of validation.
  378. return swapped_for
  379. if (
  380. "%s.%s" % (swapped_label, swapped_object.lower())
  381. != self.label_lower
  382. ):
  383. return swapped_for
  384. return None
  385. @cached_property
  386. def managers(self):
  387. managers = []
  388. seen_managers = set()
  389. bases = (b for b in self.model.mro() if hasattr(b, "_meta"))
  390. for depth, base in enumerate(bases):
  391. for manager in base._meta.local_managers:
  392. if manager.name in seen_managers:
  393. continue
  394. manager = copy.copy(manager)
  395. manager.model = self.model
  396. seen_managers.add(manager.name)
  397. managers.append((depth, manager.creation_counter, manager))
  398. return make_immutable_fields_list(
  399. "managers",
  400. (m[2] for m in sorted(managers)),
  401. )
  402. @cached_property
  403. def managers_map(self):
  404. return {manager.name: manager for manager in self.managers}
  405. @cached_property
  406. def base_manager(self):
  407. base_manager_name = self.base_manager_name
  408. if not base_manager_name:
  409. # Get the first parent's base_manager_name if there's one.
  410. for parent in self.model.mro()[1:]:
  411. if hasattr(parent, "_meta"):
  412. if parent._base_manager.name != "_base_manager":
  413. base_manager_name = parent._base_manager.name
  414. break
  415. if base_manager_name:
  416. try:
  417. return self.managers_map[base_manager_name]
  418. except KeyError:
  419. raise ValueError(
  420. "%s has no manager named %r"
  421. % (
  422. self.object_name,
  423. base_manager_name,
  424. )
  425. )
  426. manager = Manager()
  427. manager.name = "_base_manager"
  428. manager.model = self.model
  429. manager.auto_created = True
  430. return manager
  431. @cached_property
  432. def default_manager(self):
  433. default_manager_name = self.default_manager_name
  434. if not default_manager_name and not self.local_managers:
  435. # Get the first parent's default_manager_name if there's one.
  436. for parent in self.model.mro()[1:]:
  437. if hasattr(parent, "_meta"):
  438. default_manager_name = parent._meta.default_manager_name
  439. break
  440. if default_manager_name:
  441. try:
  442. return self.managers_map[default_manager_name]
  443. except KeyError:
  444. raise ValueError(
  445. "%s has no manager named %r"
  446. % (
  447. self.object_name,
  448. default_manager_name,
  449. )
  450. )
  451. if self.managers:
  452. return self.managers[0]
  453. @cached_property
  454. def fields(self):
  455. """
  456. Return a list of all forward fields on the model and its parents,
  457. excluding ManyToManyFields.
  458. Private API intended only to be used by Django itself; get_fields()
  459. combined with filtering of field properties is the public API for
  460. obtaining this field list.
  461. """
  462. # For legacy reasons, the fields property should only contain forward
  463. # fields that are not private or with a m2m cardinality. Therefore we
  464. # pass these three filters as filters to the generator.
  465. # The third lambda is a longwinded way of checking f.related_model - we don't
  466. # use that property directly because related_model is a cached property,
  467. # and all the models may not have been loaded yet; we don't want to cache
  468. # the string reference to the related_model.
  469. def is_not_an_m2m_field(f):
  470. return not (f.is_relation and f.many_to_many)
  471. def is_not_a_generic_relation(f):
  472. return not (f.is_relation and f.one_to_many)
  473. def is_not_a_generic_foreign_key(f):
  474. return not (
  475. f.is_relation
  476. and f.many_to_one
  477. and not (hasattr(f.remote_field, "model") and f.remote_field.model)
  478. )
  479. return make_immutable_fields_list(
  480. "fields",
  481. (
  482. f
  483. for f in self._get_fields(reverse=False)
  484. if is_not_an_m2m_field(f)
  485. and is_not_a_generic_relation(f)
  486. and is_not_a_generic_foreign_key(f)
  487. ),
  488. )
  489. @cached_property
  490. def concrete_fields(self):
  491. """
  492. Return a list of all concrete fields on the model and its parents.
  493. Private API intended only to be used by Django itself; get_fields()
  494. combined with filtering of field properties is the public API for
  495. obtaining this field list.
  496. """
  497. return make_immutable_fields_list(
  498. "concrete_fields", (f for f in self.fields if f.concrete)
  499. )
  500. @cached_property
  501. def local_concrete_fields(self):
  502. """
  503. Return a list of all concrete fields on the model.
  504. Private API intended only to be used by Django itself; get_fields()
  505. combined with filtering of field properties is the public API for
  506. obtaining this field list.
  507. """
  508. return make_immutable_fields_list(
  509. "local_concrete_fields", (f for f in self.local_fields if f.concrete)
  510. )
  511. @cached_property
  512. def many_to_many(self):
  513. """
  514. Return a list of all many to many fields on the model and its parents.
  515. Private API intended only to be used by Django itself; get_fields()
  516. combined with filtering of field properties is the public API for
  517. obtaining this list.
  518. """
  519. return make_immutable_fields_list(
  520. "many_to_many",
  521. (
  522. f
  523. for f in self._get_fields(reverse=False)
  524. if f.is_relation and f.many_to_many
  525. ),
  526. )
  527. @cached_property
  528. def related_objects(self):
  529. """
  530. Return all related objects pointing to the current model. The related
  531. objects can come from a one-to-one, one-to-many, or many-to-many field
  532. relation type.
  533. Private API intended only to be used by Django itself; get_fields()
  534. combined with filtering of field properties is the public API for
  535. obtaining this field list.
  536. """
  537. all_related_fields = self._get_fields(
  538. forward=False, reverse=True, include_hidden=True
  539. )
  540. return make_immutable_fields_list(
  541. "related_objects",
  542. (
  543. obj
  544. for obj in all_related_fields
  545. if not obj.hidden or obj.field.many_to_many
  546. ),
  547. )
  548. @cached_property
  549. def _forward_fields_map(self):
  550. res = {}
  551. fields = self._get_fields(reverse=False)
  552. for field in fields:
  553. res[field.name] = field
  554. # Due to the way Django's internals work, get_field() should also
  555. # be able to fetch a field by attname. In the case of a concrete
  556. # field with relation, includes the *_id name too
  557. try:
  558. res[field.attname] = field
  559. except AttributeError:
  560. pass
  561. return res
  562. @cached_property
  563. def fields_map(self):
  564. res = {}
  565. fields = self._get_fields(forward=False, include_hidden=True)
  566. for field in fields:
  567. res[field.name] = field
  568. # Due to the way Django's internals work, get_field() should also
  569. # be able to fetch a field by attname. In the case of a concrete
  570. # field with relation, includes the *_id name too
  571. try:
  572. res[field.attname] = field
  573. except AttributeError:
  574. pass
  575. return res
  576. def get_field(self, field_name):
  577. """
  578. Return a field instance given the name of a forward or reverse field.
  579. """
  580. try:
  581. # In order to avoid premature loading of the relation tree
  582. # (expensive) we prefer checking if the field is a forward field.
  583. return self._forward_fields_map[field_name]
  584. except KeyError:
  585. # If the app registry is not ready, reverse fields are
  586. # unavailable, therefore we throw a FieldDoesNotExist exception.
  587. if not self.apps.models_ready:
  588. raise FieldDoesNotExist(
  589. "%s has no field named '%s'. The app cache isn't ready yet, "
  590. "so if this is an auto-created related field, it won't "
  591. "be available yet." % (self.object_name, field_name)
  592. )
  593. try:
  594. # Retrieve field instance by name from cached or just-computed
  595. # field map.
  596. return self.fields_map[field_name]
  597. except KeyError:
  598. raise FieldDoesNotExist(
  599. "%s has no field named '%s'" % (self.object_name, field_name)
  600. )
  601. def get_base_chain(self, model):
  602. """
  603. Return a list of parent classes leading to `model` (ordered from
  604. closest to most distant ancestor). This has to handle the case where
  605. `model` is a grandparent or even more distant relation.
  606. """
  607. if not self.parents:
  608. return []
  609. if model in self.parents:
  610. return [model]
  611. for parent in self.parents:
  612. res = parent._meta.get_base_chain(model)
  613. if res:
  614. res.insert(0, parent)
  615. return res
  616. return []
  617. def get_parent_list(self):
  618. """
  619. Return all the ancestors of this model as a list ordered by MRO.
  620. Useful for determining if something is an ancestor, regardless of lineage.
  621. """
  622. result = OrderedSet(self.parents)
  623. for parent in self.parents:
  624. for ancestor in parent._meta.get_parent_list():
  625. result.add(ancestor)
  626. return list(result)
  627. def get_ancestor_link(self, ancestor):
  628. """
  629. Return the field on the current model which points to the given
  630. "ancestor". This is possible an indirect link (a pointer to a parent
  631. model, which points, eventually, to the ancestor). Used when
  632. constructing table joins for model inheritance.
  633. Return None if the model isn't an ancestor of this one.
  634. """
  635. if ancestor in self.parents:
  636. return self.parents[ancestor]
  637. for parent in self.parents:
  638. # Tries to get a link field from the immediate parent
  639. parent_link = parent._meta.get_ancestor_link(ancestor)
  640. if parent_link:
  641. # In case of a proxied model, the first link
  642. # of the chain to the ancestor is that parent
  643. # links
  644. return self.parents[parent] or parent_link
  645. def get_path_to_parent(self, parent):
  646. """
  647. Return a list of PathInfos containing the path from the current
  648. model to the parent model, or an empty list if parent is not a
  649. parent of the current model.
  650. """
  651. if self.model is parent:
  652. return []
  653. # Skip the chain of proxy to the concrete proxied model.
  654. proxied_model = self.concrete_model
  655. path = []
  656. opts = self
  657. for int_model in self.get_base_chain(parent):
  658. if int_model is proxied_model:
  659. opts = int_model._meta
  660. else:
  661. final_field = opts.parents[int_model]
  662. targets = (final_field.remote_field.get_related_field(),)
  663. opts = int_model._meta
  664. path.append(
  665. PathInfo(
  666. from_opts=final_field.model._meta,
  667. to_opts=opts,
  668. target_fields=targets,
  669. join_field=final_field,
  670. m2m=False,
  671. direct=True,
  672. filtered_relation=None,
  673. )
  674. )
  675. return path
  676. def get_path_from_parent(self, parent):
  677. """
  678. Return a list of PathInfos containing the path from the parent
  679. model to the current model, or an empty list if parent is not a
  680. parent of the current model.
  681. """
  682. if self.model is parent:
  683. return []
  684. model = self.concrete_model
  685. # Get a reversed base chain including both the current and parent
  686. # models.
  687. chain = model._meta.get_base_chain(parent)
  688. chain.reverse()
  689. chain.append(model)
  690. # Construct a list of the PathInfos between models in chain.
  691. path = []
  692. for i, ancestor in enumerate(chain[:-1]):
  693. child = chain[i + 1]
  694. link = child._meta.get_ancestor_link(ancestor)
  695. path.extend(link.reverse_path_infos)
  696. return path
  697. def _populate_directed_relation_graph(self):
  698. """
  699. This method is used by each model to find its reverse objects. As this
  700. method is very expensive and is accessed frequently (it looks up every
  701. field in a model, in every app), it is computed on first access and then
  702. is set as a property on every model.
  703. """
  704. related_objects_graph = defaultdict(list)
  705. all_models = self.apps.get_models(include_auto_created=True)
  706. for model in all_models:
  707. opts = model._meta
  708. # Abstract model's fields are copied to child models, hence we will
  709. # see the fields from the child models.
  710. if opts.abstract:
  711. continue
  712. fields_with_relations = (
  713. f
  714. for f in opts._get_fields(reverse=False, include_parents=False)
  715. if f.is_relation and f.related_model is not None
  716. )
  717. for f in fields_with_relations:
  718. if not isinstance(f.remote_field.model, str):
  719. remote_label = f.remote_field.model._meta.concrete_model._meta.label
  720. related_objects_graph[remote_label].append(f)
  721. for model in all_models:
  722. # Set the relation_tree using the internal __dict__. In this way
  723. # we avoid calling the cached property. In attribute lookup,
  724. # __dict__ takes precedence over a data descriptor (such as
  725. # @cached_property). This means that the _meta._relation_tree is
  726. # only called if related_objects is not in __dict__.
  727. related_objects = related_objects_graph[
  728. model._meta.concrete_model._meta.label
  729. ]
  730. model._meta.__dict__["_relation_tree"] = related_objects
  731. # It seems it is possible that self is not in all_models, so guard
  732. # against that with default for get().
  733. return self.__dict__.get("_relation_tree", EMPTY_RELATION_TREE)
  734. @cached_property
  735. def _relation_tree(self):
  736. return self._populate_directed_relation_graph()
  737. def _expire_cache(self, forward=True, reverse=True):
  738. # This method is usually called by apps.cache_clear(), when the
  739. # registry is finalized, or when a new field is added.
  740. if forward:
  741. for cache_key in self.FORWARD_PROPERTIES:
  742. if cache_key in self.__dict__:
  743. delattr(self, cache_key)
  744. if reverse and not self.abstract:
  745. for cache_key in self.REVERSE_PROPERTIES:
  746. if cache_key in self.__dict__:
  747. delattr(self, cache_key)
  748. self._get_fields_cache = {}
  749. def get_fields(self, include_parents=True, include_hidden=False):
  750. """
  751. Return a list of fields associated to the model. By default, include
  752. forward and reverse fields, fields derived from inheritance, but not
  753. hidden fields. The returned fields can be changed using the parameters:
  754. - include_parents: include fields derived from inheritance
  755. - include_hidden: include fields that have a related_name that
  756. starts with a "+"
  757. """
  758. if include_parents is False:
  759. include_parents = PROXY_PARENTS
  760. return self._get_fields(
  761. include_parents=include_parents, include_hidden=include_hidden
  762. )
  763. def _get_fields(
  764. self,
  765. forward=True,
  766. reverse=True,
  767. include_parents=True,
  768. include_hidden=False,
  769. seen_models=None,
  770. ):
  771. """
  772. Internal helper function to return fields of the model.
  773. * If forward=True, then fields defined on this model are returned.
  774. * If reverse=True, then relations pointing to this model are returned.
  775. * If include_hidden=True, then fields with is_hidden=True are returned.
  776. * The include_parents argument toggles if fields from parent models
  777. should be included. It has three values: True, False, and
  778. PROXY_PARENTS. When set to PROXY_PARENTS, the call will return all
  779. fields defined for the current model or any of its parents in the
  780. parent chain to the model's concrete model.
  781. """
  782. if include_parents not in (True, False, PROXY_PARENTS):
  783. raise TypeError(
  784. "Invalid argument for include_parents: %s" % (include_parents,)
  785. )
  786. # This helper function is used to allow recursion in ``get_fields()``
  787. # implementation and to provide a fast way for Django's internals to
  788. # access specific subsets of fields.
  789. # We must keep track of which models we have already seen. Otherwise we
  790. # could include the same field multiple times from different models.
  791. topmost_call = seen_models is None
  792. if topmost_call:
  793. seen_models = set()
  794. seen_models.add(self.model)
  795. # Creates a cache key composed of all arguments
  796. cache_key = (forward, reverse, include_parents, include_hidden, topmost_call)
  797. try:
  798. # In order to avoid list manipulation. Always return a shallow copy
  799. # of the results.
  800. return self._get_fields_cache[cache_key]
  801. except KeyError:
  802. pass
  803. fields = []
  804. # Recursively call _get_fields() on each parent, with the same
  805. # options provided in this call.
  806. if include_parents is not False:
  807. for parent in self.parents:
  808. # In diamond inheritance it is possible that we see the same
  809. # model from two different routes. In that case, avoid adding
  810. # fields from the same parent again.
  811. if parent in seen_models:
  812. continue
  813. if (
  814. parent._meta.concrete_model != self.concrete_model
  815. and include_parents == PROXY_PARENTS
  816. ):
  817. continue
  818. for obj in parent._meta._get_fields(
  819. forward=forward,
  820. reverse=reverse,
  821. include_parents=include_parents,
  822. include_hidden=include_hidden,
  823. seen_models=seen_models,
  824. ):
  825. if (
  826. not getattr(obj, "parent_link", False)
  827. or obj.model == self.concrete_model
  828. ):
  829. fields.append(obj)
  830. if reverse and not self.proxy:
  831. # Tree is computed once and cached until the app cache is expired.
  832. # It is composed of a list of fields pointing to the current model
  833. # from other models.
  834. all_fields = self._relation_tree
  835. for field in all_fields:
  836. # If hidden fields should be included or the relation is not
  837. # intentionally hidden, add to the fields dict.
  838. if include_hidden or not field.remote_field.hidden:
  839. fields.append(field.remote_field)
  840. if forward:
  841. fields += self.local_fields
  842. fields += self.local_many_to_many
  843. # Private fields are recopied to each child model, and they get a
  844. # different model as field.model in each child. Hence we have to
  845. # add the private fields separately from the topmost call. If we
  846. # did this recursively similar to local_fields, we would get field
  847. # instances with field.model != self.model.
  848. if topmost_call:
  849. fields += self.private_fields
  850. # In order to avoid list manipulation. Always
  851. # return a shallow copy of the results
  852. fields = make_immutable_fields_list("get_fields()", fields)
  853. # Store result into cache for later access
  854. self._get_fields_cache[cache_key] = fields
  855. return fields
  856. @cached_property
  857. def total_unique_constraints(self):
  858. """
  859. Return a list of total unique constraints. Useful for determining set
  860. of fields guaranteed to be unique for all rows.
  861. """
  862. return [
  863. constraint
  864. for constraint in self.constraints
  865. if (
  866. isinstance(constraint, UniqueConstraint)
  867. and constraint.condition is None
  868. and not constraint.contains_expressions
  869. )
  870. ]
  871. @cached_property
  872. def _property_names(self):
  873. """Return a set of the names of the properties defined on the model."""
  874. names = []
  875. for name in dir(self.model):
  876. attr = inspect.getattr_static(self.model, name)
  877. if isinstance(attr, property):
  878. names.append(name)
  879. return frozenset(names)
  880. @cached_property
  881. def db_returning_fields(self):
  882. """
  883. Private API intended only to be used by Django itself.
  884. Fields to be returned after a database insert.
  885. """
  886. return [
  887. field
  888. for field in self._get_fields(
  889. forward=True, reverse=False, include_parents=PROXY_PARENTS
  890. )
  891. if getattr(field, "db_returning", False)
  892. ]