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.
 
 
 
 

858 lines
36 KiB

  1. from __future__ import unicode_literals
  2. import warnings
  3. from bisect import bisect
  4. from collections import OrderedDict, defaultdict
  5. from itertools import chain
  6. from django.apps import apps
  7. from django.conf import settings
  8. from django.core.exceptions import FieldDoesNotExist
  9. from django.db import connections
  10. from django.db.models.fields import AutoField
  11. from django.db.models.fields.proxy import OrderWrt
  12. from django.db.models.fields.related import ManyToManyField
  13. from django.utils import six
  14. from django.utils.datastructures import ImmutableList, OrderedSet
  15. from django.utils.deprecation import RemovedInDjango110Warning
  16. from django.utils.encoding import (
  17. force_text, python_2_unicode_compatible, smart_text,
  18. )
  19. from django.utils.functional import cached_property
  20. from django.utils.lru_cache import lru_cache
  21. from django.utils.text import camel_case_to_spaces
  22. from django.utils.translation import override, string_concat
  23. PROXY_PARENTS = object()
  24. EMPTY_RELATION_TREE = tuple()
  25. IMMUTABLE_WARNING = (
  26. "The return type of '%s' should never be mutated. If you want to manipulate this list "
  27. "for your own use, make a copy first."
  28. )
  29. DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
  30. 'unique_together', 'permissions', 'get_latest_by',
  31. 'order_with_respect_to', 'app_label', 'db_tablespace',
  32. 'abstract', 'managed', 'proxy', 'swappable', 'auto_created',
  33. 'index_together', 'apps', 'default_permissions',
  34. 'select_on_save', 'default_related_name',
  35. 'required_db_features', 'required_db_vendor')
  36. class raise_deprecation(object):
  37. def __init__(self, suggested_alternative):
  38. self.suggested_alternative = suggested_alternative
  39. def __call__(self, fn):
  40. def wrapper(*args, **kwargs):
  41. warnings.warn(
  42. "'%s is an unofficial API that has been deprecated. "
  43. "You may be able to replace it with '%s'" % (
  44. fn.__name__,
  45. self.suggested_alternative,
  46. ),
  47. RemovedInDjango110Warning, stacklevel=2
  48. )
  49. return fn(*args, **kwargs)
  50. return wrapper
  51. def normalize_together(option_together):
  52. """
  53. option_together can be either a tuple of tuples, or a single
  54. tuple of two strings. Normalize it to a tuple of tuples, so that
  55. calling code can uniformly expect that.
  56. """
  57. try:
  58. if not option_together:
  59. return ()
  60. if not isinstance(option_together, (tuple, list)):
  61. raise TypeError
  62. first_element = next(iter(option_together))
  63. if not isinstance(first_element, (tuple, list)):
  64. option_together = (option_together,)
  65. # Normalize everything to tuples
  66. return tuple(tuple(ot) for ot in option_together)
  67. except TypeError:
  68. # If the value of option_together isn't valid, return it
  69. # verbatim; this will be picked up by the check framework later.
  70. return option_together
  71. def make_immutable_fields_list(name, data):
  72. return ImmutableList(data, warning=IMMUTABLE_WARNING % name)
  73. @python_2_unicode_compatible
  74. class Options(object):
  75. FORWARD_PROPERTIES = ('fields', 'many_to_many', 'concrete_fields',
  76. 'local_concrete_fields', '_forward_fields_map')
  77. REVERSE_PROPERTIES = ('related_objects', 'fields_map', '_relation_tree')
  78. def __init__(self, meta, app_label=None):
  79. self._get_fields_cache = {}
  80. self.proxied_children = []
  81. self.local_fields = []
  82. self.local_many_to_many = []
  83. self.virtual_fields = []
  84. self.model_name = None
  85. self.verbose_name = None
  86. self.verbose_name_plural = None
  87. self.db_table = ''
  88. self.ordering = []
  89. self._ordering_clash = False
  90. self.unique_together = []
  91. self.index_together = []
  92. self.select_on_save = False
  93. self.default_permissions = ('add', 'change', 'delete')
  94. self.permissions = []
  95. self.object_name = None
  96. self.app_label = app_label
  97. self.get_latest_by = None
  98. self.order_with_respect_to = None
  99. self.db_tablespace = settings.DEFAULT_TABLESPACE
  100. self.required_db_features = []
  101. self.required_db_vendor = None
  102. self.meta = meta
  103. self.pk = None
  104. self.has_auto_field = False
  105. self.auto_field = None
  106. self.abstract = False
  107. self.managed = True
  108. self.proxy = False
  109. # For any class that is a proxy (including automatically created
  110. # classes for deferred object loading), proxy_for_model tells us
  111. # which class this model is proxying. Note that proxy_for_model
  112. # can create a chain of proxy models. For non-proxy models, the
  113. # variable is always None.
  114. self.proxy_for_model = None
  115. # For any non-abstract class, the concrete class is the model
  116. # in the end of the proxy_for_model chain. In particular, for
  117. # concrete models, the concrete_model is always the class itself.
  118. self.concrete_model = None
  119. self.swappable = None
  120. self.parents = OrderedDict()
  121. self.auto_created = False
  122. # To handle various inheritance situations, we need to track where
  123. # managers came from (concrete or abstract base classes). `managers`
  124. # keeps a list of 3-tuples of the form:
  125. # (creation_counter, instance, abstract(=True))
  126. self.managers = []
  127. # List of all lookups defined in ForeignKey 'limit_choices_to' options
  128. # from *other* models. Needed for some admin checks. Internal use only.
  129. self.related_fkey_lookups = []
  130. # A custom app registry to use, if you're making a separate model set.
  131. self.apps = apps
  132. self.default_related_name = None
  133. @lru_cache(maxsize=None)
  134. def _map_model(self, link):
  135. # This helper function is used to allow backwards compatibility with
  136. # the previous API. No future methods should use this function.
  137. # It maps a field to (field, model or related_model,) depending on the
  138. # field type.
  139. model = link.model._meta.concrete_model
  140. if model is self.model:
  141. model = None
  142. return link, model
  143. @lru_cache(maxsize=None)
  144. def _map_model_details(self, link):
  145. # This helper function is used to allow backwards compatibility with
  146. # the previous API. No future methods should use this function.
  147. # This function maps a field to a tuple of:
  148. # (field, model or related_model, direct, is_m2m) depending on the
  149. # field type.
  150. direct = not link.auto_created or link.concrete
  151. model = link.model._meta.concrete_model
  152. if model is self.model:
  153. model = None
  154. m2m = link.is_relation and link.many_to_many
  155. return link, model, direct, m2m
  156. @property
  157. def label(self):
  158. return '%s.%s' % (self.app_label, self.object_name)
  159. @property
  160. def label_lower(self):
  161. return '%s.%s' % (self.app_label, self.model_name)
  162. @property
  163. def app_config(self):
  164. # Don't go through get_app_config to avoid triggering imports.
  165. return self.apps.app_configs.get(self.app_label)
  166. @property
  167. def installed(self):
  168. return self.app_config is not None
  169. @property
  170. def abstract_managers(self):
  171. return [
  172. (counter, instance.name, instance) for counter, instance, abstract
  173. in self.managers if abstract
  174. ]
  175. @property
  176. def concrete_managers(self):
  177. return [
  178. (counter, instance.name, instance) for counter, instance, abstract
  179. in self.managers if not abstract
  180. ]
  181. def contribute_to_class(self, cls, name):
  182. from django.db import connection
  183. from django.db.backends.utils import truncate_name
  184. cls._meta = self
  185. self.model = cls
  186. # First, construct the default values for these options.
  187. self.object_name = cls.__name__
  188. self.model_name = self.object_name.lower()
  189. self.verbose_name = camel_case_to_spaces(self.object_name)
  190. # Store the original user-defined values for each option,
  191. # for use when serializing the model definition
  192. self.original_attrs = {}
  193. # Next, apply any overridden values from 'class Meta'.
  194. if self.meta:
  195. meta_attrs = self.meta.__dict__.copy()
  196. for name in self.meta.__dict__:
  197. # Ignore any private attributes that Django doesn't care about.
  198. # NOTE: We can't modify a dictionary's contents while looping
  199. # over it, so we loop over the *original* dictionary instead.
  200. if name.startswith('_'):
  201. del meta_attrs[name]
  202. for attr_name in DEFAULT_NAMES:
  203. if attr_name in meta_attrs:
  204. setattr(self, attr_name, meta_attrs.pop(attr_name))
  205. self.original_attrs[attr_name] = getattr(self, attr_name)
  206. elif hasattr(self.meta, attr_name):
  207. setattr(self, attr_name, getattr(self.meta, attr_name))
  208. self.original_attrs[attr_name] = getattr(self, attr_name)
  209. self.unique_together = normalize_together(self.unique_together)
  210. self.index_together = normalize_together(self.index_together)
  211. # verbose_name_plural is a special case because it uses a 's'
  212. # by default.
  213. if self.verbose_name_plural is None:
  214. self.verbose_name_plural = string_concat(self.verbose_name, 's')
  215. # order_with_respect_and ordering are mutually exclusive.
  216. self._ordering_clash = bool(self.ordering and self.order_with_respect_to)
  217. # Any leftover attributes must be invalid.
  218. if meta_attrs != {}:
  219. raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
  220. else:
  221. self.verbose_name_plural = string_concat(self.verbose_name, 's')
  222. del self.meta
  223. # If the db_table wasn't provided, use the app_label + model_name.
  224. if not self.db_table:
  225. self.db_table = "%s_%s" % (self.app_label, self.model_name)
  226. self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  227. def _prepare(self, model):
  228. if self.order_with_respect_to:
  229. # The app registry will not be ready at this point, so we cannot
  230. # use get_field().
  231. query = self.order_with_respect_to
  232. try:
  233. self.order_with_respect_to = next(
  234. f for f in self._get_fields(reverse=False)
  235. if f.name == query or f.attname == query
  236. )
  237. except StopIteration:
  238. raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, query))
  239. self.ordering = ('_order',)
  240. if not any(isinstance(field, OrderWrt) for field in model._meta.local_fields):
  241. model.add_to_class('_order', OrderWrt())
  242. else:
  243. self.order_with_respect_to = None
  244. if self.pk is None:
  245. if self.parents:
  246. # Promote the first parent link in lieu of adding yet another
  247. # field.
  248. field = next(six.itervalues(self.parents))
  249. # Look for a local field with the same name as the
  250. # first parent link. If a local field has already been
  251. # created, use it instead of promoting the parent
  252. already_created = [fld for fld in self.local_fields if fld.name == field.name]
  253. if already_created:
  254. field = already_created[0]
  255. field.primary_key = True
  256. self.setup_pk(field)
  257. else:
  258. auto = AutoField(verbose_name='ID', primary_key=True,
  259. auto_created=True)
  260. model.add_to_class('id', auto)
  261. def add_field(self, field, virtual=False):
  262. # Insert the given field in the order in which it was created, using
  263. # the "creation_counter" attribute of the field.
  264. # Move many-to-many related fields from self.fields into
  265. # self.many_to_many.
  266. if virtual:
  267. self.virtual_fields.append(field)
  268. elif field.is_relation and field.many_to_many:
  269. self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field)
  270. else:
  271. self.local_fields.insert(bisect(self.local_fields, field), field)
  272. self.setup_pk(field)
  273. # If the field being added is a relation to another known field,
  274. # expire the cache on this field and the forward cache on the field
  275. # being referenced, because there will be new relationships in the
  276. # cache. Otherwise, expire the cache of references *to* this field.
  277. # The mechanism for getting at the related model is slightly odd -
  278. # ideally, we'd just ask for field.related_model. However, related_model
  279. # is a cached property, and all the models haven't been loaded yet, so
  280. # we need to make sure we don't cache a string reference.
  281. if field.is_relation and hasattr(field.remote_field, 'model') and field.remote_field.model:
  282. try:
  283. field.remote_field.model._meta._expire_cache(forward=False)
  284. except AttributeError:
  285. pass
  286. self._expire_cache()
  287. else:
  288. self._expire_cache(reverse=False)
  289. def setup_pk(self, field):
  290. if not self.pk and field.primary_key:
  291. self.pk = field
  292. field.serialize = False
  293. def setup_proxy(self, target):
  294. """
  295. Does the internal setup so that the current model is a proxy for
  296. "target".
  297. """
  298. self.pk = target._meta.pk
  299. self.proxy_for_model = target
  300. self.db_table = target._meta.db_table
  301. def __repr__(self):
  302. return '<Options for %s>' % self.object_name
  303. def __str__(self):
  304. return "%s.%s" % (smart_text(self.app_label), smart_text(self.model_name))
  305. def can_migrate(self, connection):
  306. """
  307. Return True if the model can/should be migrated on the `connection`.
  308. `connection` can be either a real connection or a connection alias.
  309. """
  310. if self.proxy or self.swapped or not self.managed:
  311. return False
  312. if isinstance(connection, six.string_types):
  313. connection = connections[connection]
  314. if self.required_db_vendor:
  315. return self.required_db_vendor == connection.vendor
  316. if self.required_db_features:
  317. return all(getattr(connection.features, feat, False)
  318. for feat in self.required_db_features)
  319. return True
  320. @property
  321. def verbose_name_raw(self):
  322. """
  323. There are a few places where the untranslated verbose name is needed
  324. (so that we get the same value regardless of currently active
  325. locale).
  326. """
  327. with override(None):
  328. return force_text(self.verbose_name)
  329. @property
  330. def swapped(self):
  331. """
  332. Has this model been swapped out for another? If so, return the model
  333. name of the replacement; otherwise, return None.
  334. For historical reasons, model name lookups using get_model() are
  335. case insensitive, so we make sure we are case insensitive here.
  336. """
  337. if self.swappable:
  338. swapped_for = getattr(settings, self.swappable, None)
  339. if swapped_for:
  340. try:
  341. swapped_label, swapped_object = swapped_for.split('.')
  342. except ValueError:
  343. # setting not in the format app_label.model_name
  344. # raising ImproperlyConfigured here causes problems with
  345. # test cleanup code - instead it is raised in get_user_model
  346. # or as part of validation.
  347. return swapped_for
  348. if '%s.%s' % (swapped_label, swapped_object.lower()) != self.label_lower:
  349. return swapped_for
  350. return None
  351. @cached_property
  352. def fields(self):
  353. """
  354. Returns a list of all forward fields on the model and its parents,
  355. excluding ManyToManyFields.
  356. Private API intended only to be used by Django itself; get_fields()
  357. combined with filtering of field properties is the public API for
  358. obtaining this field list.
  359. """
  360. # For legacy reasons, the fields property should only contain forward
  361. # fields that are not virtual or with a m2m cardinality. Therefore we
  362. # pass these three filters as filters to the generator.
  363. # The third lambda is a longwinded way of checking f.related_model - we don't
  364. # use that property directly because related_model is a cached property,
  365. # and all the models may not have been loaded yet; we don't want to cache
  366. # the string reference to the related_model.
  367. is_not_an_m2m_field = lambda f: not (f.is_relation and f.many_to_many)
  368. is_not_a_generic_relation = lambda f: not (f.is_relation and f.one_to_many)
  369. is_not_a_generic_foreign_key = lambda f: not (
  370. f.is_relation and f.many_to_one and not (hasattr(f.remote_field, 'model') and f.remote_field.model)
  371. )
  372. return make_immutable_fields_list(
  373. "fields",
  374. (f for f in self._get_fields(reverse=False) if
  375. is_not_an_m2m_field(f) and is_not_a_generic_relation(f)
  376. and is_not_a_generic_foreign_key(f))
  377. )
  378. @cached_property
  379. def concrete_fields(self):
  380. """
  381. Returns a list of all concrete fields on the model and its parents.
  382. Private API intended only to be used by Django itself; get_fields()
  383. combined with filtering of field properties is the public API for
  384. obtaining this field list.
  385. """
  386. return make_immutable_fields_list(
  387. "concrete_fields", (f for f in self.fields if f.concrete)
  388. )
  389. @cached_property
  390. def local_concrete_fields(self):
  391. """
  392. Returns a list of all concrete fields on the model.
  393. Private API intended only to be used by Django itself; get_fields()
  394. combined with filtering of field properties is the public API for
  395. obtaining this field list.
  396. """
  397. return make_immutable_fields_list(
  398. "local_concrete_fields", (f for f in self.local_fields if f.concrete)
  399. )
  400. @raise_deprecation(suggested_alternative="get_fields()")
  401. def get_fields_with_model(self):
  402. return [self._map_model(f) for f in self.get_fields()]
  403. @raise_deprecation(suggested_alternative="get_fields()")
  404. def get_concrete_fields_with_model(self):
  405. return [self._map_model(f) for f in self.concrete_fields]
  406. @cached_property
  407. def many_to_many(self):
  408. """
  409. Returns a list of all many to many fields on the model and its parents.
  410. Private API intended only to be used by Django itself; get_fields()
  411. combined with filtering of field properties is the public API for
  412. obtaining this list.
  413. """
  414. return make_immutable_fields_list(
  415. "many_to_many",
  416. (f for f in self._get_fields(reverse=False)
  417. if f.is_relation and f.many_to_many)
  418. )
  419. @cached_property
  420. def related_objects(self):
  421. """
  422. Returns all related objects pointing to the current model. The related
  423. objects can come from a one-to-one, one-to-many, or many-to-many field
  424. relation type.
  425. Private API intended only to be used by Django itself; get_fields()
  426. combined with filtering of field properties is the public API for
  427. obtaining this field list.
  428. """
  429. all_related_fields = self._get_fields(forward=False, reverse=True, include_hidden=True)
  430. return make_immutable_fields_list(
  431. "related_objects",
  432. (obj for obj in all_related_fields
  433. if not obj.hidden or obj.field.many_to_many)
  434. )
  435. @raise_deprecation(suggested_alternative="get_fields()")
  436. def get_m2m_with_model(self):
  437. return [self._map_model(f) for f in self.many_to_many]
  438. @cached_property
  439. def _forward_fields_map(self):
  440. res = {}
  441. fields = self._get_fields(reverse=False)
  442. for field in fields:
  443. res[field.name] = field
  444. # Due to the way Django's internals work, get_field() should also
  445. # be able to fetch a field by attname. In the case of a concrete
  446. # field with relation, includes the *_id name too
  447. try:
  448. res[field.attname] = field
  449. except AttributeError:
  450. pass
  451. return res
  452. @cached_property
  453. def fields_map(self):
  454. res = {}
  455. fields = self._get_fields(forward=False, include_hidden=True)
  456. for field in fields:
  457. res[field.name] = field
  458. # Due to the way Django's internals work, get_field() should also
  459. # be able to fetch a field by attname. In the case of a concrete
  460. # field with relation, includes the *_id name too
  461. try:
  462. res[field.attname] = field
  463. except AttributeError:
  464. pass
  465. return res
  466. def get_field(self, field_name, many_to_many=None):
  467. """
  468. Returns a field instance given a field name. The field can be either a
  469. forward or reverse field, unless many_to_many is specified; if it is,
  470. only forward fields will be returned.
  471. The many_to_many argument exists for backwards compatibility reasons;
  472. it has been deprecated and will be removed in Django 1.10.
  473. """
  474. m2m_in_kwargs = many_to_many is not None
  475. if m2m_in_kwargs:
  476. # Always throw a warning if many_to_many is used regardless of
  477. # whether it alters the return type or not.
  478. warnings.warn(
  479. "The 'many_to_many' argument on get_field() is deprecated; "
  480. "use a filter on field.many_to_many instead.",
  481. RemovedInDjango110Warning
  482. )
  483. try:
  484. # In order to avoid premature loading of the relation tree
  485. # (expensive) we prefer checking if the field is a forward field.
  486. field = self._forward_fields_map[field_name]
  487. if many_to_many is False and field.many_to_many:
  488. raise FieldDoesNotExist(
  489. '%s has no field named %r' % (self.object_name, field_name)
  490. )
  491. return field
  492. except KeyError:
  493. # If the app registry is not ready, reverse fields are
  494. # unavailable, therefore we throw a FieldDoesNotExist exception.
  495. if not self.apps.models_ready:
  496. raise FieldDoesNotExist(
  497. "%s has no field named %r. The app cache isn't ready yet, "
  498. "so if this is an auto-created related field, it won't "
  499. "be available yet." % (self.object_name, field_name)
  500. )
  501. try:
  502. if m2m_in_kwargs:
  503. # Previous API does not allow searching reverse fields.
  504. raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
  505. # Retrieve field instance by name from cached or just-computed
  506. # field map.
  507. return self.fields_map[field_name]
  508. except KeyError:
  509. raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
  510. @raise_deprecation(suggested_alternative="get_field()")
  511. def get_field_by_name(self, name):
  512. return self._map_model_details(self.get_field(name))
  513. @raise_deprecation(suggested_alternative="get_fields()")
  514. def get_all_field_names(self):
  515. names = set()
  516. fields = self.get_fields()
  517. for field in fields:
  518. # For backwards compatibility GenericForeignKey should not be
  519. # included in the results.
  520. if field.is_relation and field.many_to_one and field.related_model is None:
  521. continue
  522. # Relations to child proxy models should not be included.
  523. if (field.model != self.model and
  524. field.model._meta.concrete_model == self.concrete_model):
  525. continue
  526. names.add(field.name)
  527. if hasattr(field, 'attname'):
  528. names.add(field.attname)
  529. return list(names)
  530. @raise_deprecation(suggested_alternative="get_fields()")
  531. def get_all_related_objects(self, local_only=False, include_hidden=False,
  532. include_proxy_eq=False):
  533. include_parents = True if local_only is False else PROXY_PARENTS
  534. fields = self._get_fields(
  535. forward=False, reverse=True,
  536. include_parents=include_parents,
  537. include_hidden=include_hidden,
  538. )
  539. fields = (obj for obj in fields if not isinstance(obj.field, ManyToManyField))
  540. if include_proxy_eq:
  541. children = chain.from_iterable(c._relation_tree
  542. for c in self.concrete_model._meta.proxied_children
  543. if c is not self)
  544. relations = (f.remote_field for f in children
  545. if include_hidden or not f.remote_field.field.remote_field.is_hidden())
  546. fields = chain(fields, relations)
  547. return list(fields)
  548. @raise_deprecation(suggested_alternative="get_fields()")
  549. def get_all_related_objects_with_model(self, local_only=False, include_hidden=False,
  550. include_proxy_eq=False):
  551. return [
  552. self._map_model(f) for f in self.get_all_related_objects(
  553. local_only=local_only,
  554. include_hidden=include_hidden,
  555. include_proxy_eq=include_proxy_eq,
  556. )
  557. ]
  558. @raise_deprecation(suggested_alternative="get_fields()")
  559. def get_all_related_many_to_many_objects(self, local_only=False):
  560. include_parents = True if local_only is not True else PROXY_PARENTS
  561. fields = self._get_fields(
  562. forward=False, reverse=True,
  563. include_parents=include_parents, include_hidden=True
  564. )
  565. return [obj for obj in fields if isinstance(obj.field, ManyToManyField)]
  566. @raise_deprecation(suggested_alternative="get_fields()")
  567. def get_all_related_m2m_objects_with_model(self):
  568. fields = self._get_fields(forward=False, reverse=True, include_hidden=True)
  569. return [self._map_model(obj) for obj in fields if isinstance(obj.field, ManyToManyField)]
  570. def get_base_chain(self, model):
  571. """
  572. Return a list of parent classes leading to `model` (ordered from
  573. closest to most distant ancestor). This has to handle the case where
  574. `model` is a grandparent or even more distant relation.
  575. """
  576. if not self.parents:
  577. return []
  578. if model in self.parents:
  579. return [model]
  580. for parent in self.parents:
  581. res = parent._meta.get_base_chain(model)
  582. if res:
  583. res.insert(0, parent)
  584. return res
  585. return []
  586. def get_parent_list(self):
  587. """
  588. Returns all the ancestors of this model as a list ordered by MRO.
  589. Useful for determining if something is an ancestor, regardless of lineage.
  590. """
  591. result = OrderedSet(self.parents)
  592. for parent in self.parents:
  593. for ancestor in parent._meta.get_parent_list():
  594. result.add(ancestor)
  595. return list(result)
  596. def get_ancestor_link(self, ancestor):
  597. """
  598. Returns the field on the current model which points to the given
  599. "ancestor". This is possible an indirect link (a pointer to a parent
  600. model, which points, eventually, to the ancestor). Used when
  601. constructing table joins for model inheritance.
  602. Returns None if the model isn't an ancestor of this one.
  603. """
  604. if ancestor in self.parents:
  605. return self.parents[ancestor]
  606. for parent in self.parents:
  607. # Tries to get a link field from the immediate parent
  608. parent_link = parent._meta.get_ancestor_link(ancestor)
  609. if parent_link:
  610. # In case of a proxied model, the first link
  611. # of the chain to the ancestor is that parent
  612. # links
  613. return self.parents[parent] or parent_link
  614. def _populate_directed_relation_graph(self):
  615. """
  616. This method is used by each model to find its reverse objects. As this
  617. method is very expensive and is accessed frequently (it looks up every
  618. field in a model, in every app), it is computed on first access and then
  619. is set as a property on every model.
  620. """
  621. related_objects_graph = defaultdict(list)
  622. all_models = self.apps.get_models(include_auto_created=True)
  623. for model in all_models:
  624. # Abstract model's fields are copied to child models, hence we will
  625. # see the fields from the child models.
  626. if model._meta.abstract:
  627. continue
  628. fields_with_relations = (
  629. f for f in model._meta._get_fields(reverse=False, include_parents=False)
  630. if f.is_relation and f.related_model is not None
  631. )
  632. for f in fields_with_relations:
  633. if not isinstance(f.remote_field.model, six.string_types):
  634. related_objects_graph[f.remote_field.model._meta].append(f)
  635. for model in all_models:
  636. # Set the relation_tree using the internal __dict__. In this way
  637. # we avoid calling the cached property. In attribute lookup,
  638. # __dict__ takes precedence over a data descriptor (such as
  639. # @cached_property). This means that the _meta._relation_tree is
  640. # only called if related_objects is not in __dict__.
  641. related_objects = related_objects_graph[model._meta]
  642. model._meta.__dict__['_relation_tree'] = related_objects
  643. # It seems it is possible that self is not in all_models, so guard
  644. # against that with default for get().
  645. return self.__dict__.get('_relation_tree', EMPTY_RELATION_TREE)
  646. @cached_property
  647. def _relation_tree(self):
  648. return self._populate_directed_relation_graph()
  649. def _expire_cache(self, forward=True, reverse=True):
  650. # This method is usually called by apps.cache_clear(), when the
  651. # registry is finalized, or when a new field is added.
  652. properties_to_expire = []
  653. if forward:
  654. properties_to_expire.extend(self.FORWARD_PROPERTIES)
  655. if reverse and not self.abstract:
  656. properties_to_expire.extend(self.REVERSE_PROPERTIES)
  657. for cache_key in properties_to_expire:
  658. try:
  659. delattr(self, cache_key)
  660. except AttributeError:
  661. pass
  662. self._get_fields_cache = {}
  663. def get_fields(self, include_parents=True, include_hidden=False):
  664. """
  665. Returns a list of fields associated to the model. By default, includes
  666. forward and reverse fields, fields derived from inheritance, but not
  667. hidden fields. The returned fields can be changed using the parameters:
  668. - include_parents: include fields derived from inheritance
  669. - include_hidden: include fields that have a related_name that
  670. starts with a "+"
  671. """
  672. if include_parents is False:
  673. include_parents = PROXY_PARENTS
  674. return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
  675. def _get_fields(self, forward=True, reverse=True, include_parents=True, include_hidden=False,
  676. seen_models=None):
  677. """
  678. Internal helper function to return fields of the model.
  679. * If forward=True, then fields defined on this model are returned.
  680. * If reverse=True, then relations pointing to this model are returned.
  681. * If include_hidden=True, then fields with is_hidden=True are returned.
  682. * The include_parents argument toggles if fields from parent models
  683. should be included. It has three values: True, False, and
  684. PROXY_PARENTS. When set to PROXY_PARENTS, the call will return all
  685. fields defined for the current model or any of its parents in the
  686. parent chain to the model's concrete model.
  687. """
  688. if include_parents not in (True, False, PROXY_PARENTS):
  689. raise TypeError("Invalid argument for include_parents: %s" % (include_parents,))
  690. # This helper function is used to allow recursion in ``get_fields()``
  691. # implementation and to provide a fast way for Django's internals to
  692. # access specific subsets of fields.
  693. # We must keep track of which models we have already seen. Otherwise we
  694. # could include the same field multiple times from different models.
  695. topmost_call = False
  696. if seen_models is None:
  697. seen_models = set()
  698. topmost_call = True
  699. seen_models.add(self.model)
  700. # Creates a cache key composed of all arguments
  701. cache_key = (forward, reverse, include_parents, include_hidden, topmost_call)
  702. try:
  703. # In order to avoid list manipulation. Always return a shallow copy
  704. # of the results.
  705. return self._get_fields_cache[cache_key]
  706. except KeyError:
  707. pass
  708. fields = []
  709. # Recursively call _get_fields() on each parent, with the same
  710. # options provided in this call.
  711. if include_parents is not False:
  712. for parent in self.parents:
  713. # In diamond inheritance it is possible that we see the same
  714. # model from two different routes. In that case, avoid adding
  715. # fields from the same parent again.
  716. if parent in seen_models:
  717. continue
  718. if (parent._meta.concrete_model != self.concrete_model and
  719. include_parents == PROXY_PARENTS):
  720. continue
  721. for obj in parent._meta._get_fields(
  722. forward=forward, reverse=reverse, include_parents=include_parents,
  723. include_hidden=include_hidden, seen_models=seen_models):
  724. if hasattr(obj, 'parent_link') and obj.parent_link:
  725. continue
  726. fields.append(obj)
  727. if reverse:
  728. # Tree is computed once and cached until the app cache is expired.
  729. # It is composed of a list of fields pointing to the current model
  730. # from other models.
  731. all_fields = self._relation_tree
  732. for field in all_fields:
  733. # If hidden fields should be included or the relation is not
  734. # intentionally hidden, add to the fields dict.
  735. if include_hidden or not field.remote_field.hidden:
  736. fields.append(field.remote_field)
  737. if forward:
  738. fields.extend(
  739. field for field in chain(self.local_fields, self.local_many_to_many)
  740. )
  741. # Virtual fields are recopied to each child model, and they get a
  742. # different model as field.model in each child. Hence we have to
  743. # add the virtual fields separately from the topmost call. If we
  744. # did this recursively similar to local_fields, we would get field
  745. # instances with field.model != self.model.
  746. if topmost_call:
  747. fields.extend(
  748. f for f in self.virtual_fields
  749. )
  750. # In order to avoid list manipulation. Always
  751. # return a shallow copy of the results
  752. fields = make_immutable_fields_list("get_fields()", fields)
  753. # Store result into cache for later access
  754. self._get_fields_cache[cache_key] = fields
  755. return fields