query_utils.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. """
  2. Various data structures used in query construction.
  3. Factored out from django.db.models.query to avoid making the main module very
  4. large and/or so that they can be used by other modules without getting into
  5. circular import difficulties.
  6. """
  7. import functools
  8. import inspect
  9. import logging
  10. from collections import namedtuple
  11. from django.core.exceptions import FieldError
  12. from django.db import DEFAULT_DB_ALIAS, DatabaseError, connections
  13. from django.db.models.constants import LOOKUP_SEP
  14. from django.utils import tree
  15. from django.utils.functional import cached_property
  16. from django.utils.hashable import make_hashable
  17. logger = logging.getLogger("django.db.models")
  18. # PathInfo is used when converting lookups (fk__somecol). The contents
  19. # describe the relation in Model terms (model Options and Fields for both
  20. # sides of the relation. The join_field is the field backing the relation.
  21. PathInfo = namedtuple(
  22. "PathInfo",
  23. "from_opts to_opts target_fields join_field m2m direct filtered_relation",
  24. )
  25. def subclasses(cls):
  26. yield cls
  27. for subclass in cls.__subclasses__():
  28. yield from subclasses(subclass)
  29. class Q(tree.Node):
  30. """
  31. Encapsulate filters as objects that can then be combined logically (using
  32. `&` and `|`).
  33. """
  34. # Connection types
  35. AND = "AND"
  36. OR = "OR"
  37. XOR = "XOR"
  38. default = AND
  39. conditional = True
  40. def __init__(self, *args, _connector=None, _negated=False, **kwargs):
  41. super().__init__(
  42. children=[*args, *sorted(kwargs.items())],
  43. connector=_connector,
  44. negated=_negated,
  45. )
  46. def _combine(self, other, conn):
  47. if getattr(other, "conditional", False) is False:
  48. raise TypeError(other)
  49. if not self:
  50. return other.copy()
  51. if not other and isinstance(other, Q):
  52. return self.copy()
  53. obj = self.create(connector=conn)
  54. obj.add(self, conn)
  55. obj.add(other, conn)
  56. return obj
  57. def __or__(self, other):
  58. return self._combine(other, self.OR)
  59. def __and__(self, other):
  60. return self._combine(other, self.AND)
  61. def __xor__(self, other):
  62. return self._combine(other, self.XOR)
  63. def __invert__(self):
  64. obj = self.copy()
  65. obj.negate()
  66. return obj
  67. def resolve_expression(
  68. self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
  69. ):
  70. # We must promote any new joins to left outer joins so that when Q is
  71. # used as an expression, rows aren't filtered due to joins.
  72. clause, joins = query._add_q(
  73. self,
  74. reuse,
  75. allow_joins=allow_joins,
  76. split_subq=False,
  77. check_filterable=False,
  78. summarize=summarize,
  79. )
  80. query.promote_joins(joins)
  81. return clause
  82. def flatten(self):
  83. """
  84. Recursively yield this Q object and all subexpressions, in depth-first
  85. order.
  86. """
  87. yield self
  88. for child in self.children:
  89. if isinstance(child, tuple):
  90. # Use the lookup.
  91. child = child[1]
  92. if hasattr(child, "flatten"):
  93. yield from child.flatten()
  94. else:
  95. yield child
  96. def check(self, against, using=DEFAULT_DB_ALIAS):
  97. """
  98. Do a database query to check if the expressions of the Q instance
  99. matches against the expressions.
  100. """
  101. # Avoid circular imports.
  102. from django.db.models import BooleanField, Value
  103. from django.db.models.functions import Coalesce
  104. from django.db.models.sql import Query
  105. from django.db.models.sql.constants import SINGLE
  106. query = Query(None)
  107. for name, value in against.items():
  108. if not hasattr(value, "resolve_expression"):
  109. value = Value(value)
  110. query.add_annotation(value, name, select=False)
  111. query.add_annotation(Value(1), "_check")
  112. # This will raise a FieldError if a field is missing in "against".
  113. if connections[using].features.supports_comparing_boolean_expr:
  114. query.add_q(Q(Coalesce(self, True, output_field=BooleanField())))
  115. else:
  116. query.add_q(self)
  117. compiler = query.get_compiler(using=using)
  118. try:
  119. return compiler.execute_sql(SINGLE) is not None
  120. except DatabaseError as e:
  121. logger.warning("Got a database error calling check() on %r: %s", self, e)
  122. return True
  123. def deconstruct(self):
  124. path = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
  125. if path.startswith("django.db.models.query_utils"):
  126. path = path.replace("django.db.models.query_utils", "django.db.models")
  127. args = tuple(self.children)
  128. kwargs = {}
  129. if self.connector != self.default:
  130. kwargs["_connector"] = self.connector
  131. if self.negated:
  132. kwargs["_negated"] = True
  133. return path, args, kwargs
  134. @cached_property
  135. def identity(self):
  136. path, args, kwargs = self.deconstruct()
  137. identity = [path, *kwargs.items()]
  138. for child in args:
  139. if isinstance(child, tuple):
  140. arg, value = child
  141. value = make_hashable(value)
  142. identity.append((arg, value))
  143. else:
  144. identity.append(child)
  145. return tuple(identity)
  146. def __eq__(self, other):
  147. if not isinstance(other, Q):
  148. return NotImplemented
  149. return other.identity == self.identity
  150. def __hash__(self):
  151. return hash(self.identity)
  152. class DeferredAttribute:
  153. """
  154. A wrapper for a deferred-loading field. When the value is read from this
  155. object the first time, the query is executed.
  156. """
  157. def __init__(self, field):
  158. self.field = field
  159. def __get__(self, instance, cls=None):
  160. """
  161. Retrieve and caches the value from the datastore on the first lookup.
  162. Return the cached value.
  163. """
  164. if instance is None:
  165. return self
  166. data = instance.__dict__
  167. field_name = self.field.attname
  168. if field_name not in data:
  169. # Let's see if the field is part of the parent chain. If so we
  170. # might be able to reuse the already loaded value. Refs #18343.
  171. val = self._check_parent_chain(instance)
  172. if val is None:
  173. if instance.pk is None and self.field.generated:
  174. raise AttributeError(
  175. "Cannot read a generated field from an unsaved model."
  176. )
  177. instance.refresh_from_db(fields=[field_name])
  178. else:
  179. data[field_name] = val
  180. return data[field_name]
  181. def _check_parent_chain(self, instance):
  182. """
  183. Check if the field value can be fetched from a parent field already
  184. loaded in the instance. This can be done if the to-be fetched
  185. field is a primary key field.
  186. """
  187. opts = instance._meta
  188. link_field = opts.get_ancestor_link(self.field.model)
  189. if self.field.primary_key and self.field != link_field:
  190. return getattr(instance, link_field.attname)
  191. return None
  192. class class_or_instance_method:
  193. """
  194. Hook used in RegisterLookupMixin to return partial functions depending on
  195. the caller type (instance or class of models.Field).
  196. """
  197. def __init__(self, class_method, instance_method):
  198. self.class_method = class_method
  199. self.instance_method = instance_method
  200. def __get__(self, instance, owner):
  201. if instance is None:
  202. return functools.partial(self.class_method, owner)
  203. return functools.partial(self.instance_method, instance)
  204. class RegisterLookupMixin:
  205. def _get_lookup(self, lookup_name):
  206. return self.get_lookups().get(lookup_name, None)
  207. @functools.cache
  208. def get_class_lookups(cls):
  209. class_lookups = [
  210. parent.__dict__.get("class_lookups", {}) for parent in inspect.getmro(cls)
  211. ]
  212. return cls.merge_dicts(class_lookups)
  213. def get_instance_lookups(self):
  214. class_lookups = self.get_class_lookups()
  215. if instance_lookups := getattr(self, "instance_lookups", None):
  216. return {**class_lookups, **instance_lookups}
  217. return class_lookups
  218. get_lookups = class_or_instance_method(get_class_lookups, get_instance_lookups)
  219. get_class_lookups = classmethod(get_class_lookups)
  220. def get_lookup(self, lookup_name):
  221. from django.db.models.lookups import Lookup
  222. found = self._get_lookup(lookup_name)
  223. if found is None and hasattr(self, "output_field"):
  224. return self.output_field.get_lookup(lookup_name)
  225. if found is not None and not issubclass(found, Lookup):
  226. return None
  227. return found
  228. def get_transform(self, lookup_name):
  229. from django.db.models.lookups import Transform
  230. found = self._get_lookup(lookup_name)
  231. if found is None and hasattr(self, "output_field"):
  232. return self.output_field.get_transform(lookup_name)
  233. if found is not None and not issubclass(found, Transform):
  234. return None
  235. return found
  236. @staticmethod
  237. def merge_dicts(dicts):
  238. """
  239. Merge dicts in reverse to preference the order of the original list. e.g.,
  240. merge_dicts([a, b]) will preference the keys in 'a' over those in 'b'.
  241. """
  242. merged = {}
  243. for d in reversed(dicts):
  244. merged.update(d)
  245. return merged
  246. @classmethod
  247. def _clear_cached_class_lookups(cls):
  248. for subclass in subclasses(cls):
  249. subclass.get_class_lookups.cache_clear()
  250. def register_class_lookup(cls, lookup, lookup_name=None):
  251. if lookup_name is None:
  252. lookup_name = lookup.lookup_name
  253. if "class_lookups" not in cls.__dict__:
  254. cls.class_lookups = {}
  255. cls.class_lookups[lookup_name] = lookup
  256. cls._clear_cached_class_lookups()
  257. return lookup
  258. def register_instance_lookup(self, lookup, lookup_name=None):
  259. if lookup_name is None:
  260. lookup_name = lookup.lookup_name
  261. if "instance_lookups" not in self.__dict__:
  262. self.instance_lookups = {}
  263. self.instance_lookups[lookup_name] = lookup
  264. return lookup
  265. register_lookup = class_or_instance_method(
  266. register_class_lookup, register_instance_lookup
  267. )
  268. register_class_lookup = classmethod(register_class_lookup)
  269. def _unregister_class_lookup(cls, lookup, lookup_name=None):
  270. """
  271. Remove given lookup from cls lookups. For use in tests only as it's
  272. not thread-safe.
  273. """
  274. if lookup_name is None:
  275. lookup_name = lookup.lookup_name
  276. del cls.class_lookups[lookup_name]
  277. cls._clear_cached_class_lookups()
  278. def _unregister_instance_lookup(self, lookup, lookup_name=None):
  279. """
  280. Remove given lookup from instance lookups. For use in tests only as
  281. it's not thread-safe.
  282. """
  283. if lookup_name is None:
  284. lookup_name = lookup.lookup_name
  285. del self.instance_lookups[lookup_name]
  286. _unregister_lookup = class_or_instance_method(
  287. _unregister_class_lookup, _unregister_instance_lookup
  288. )
  289. _unregister_class_lookup = classmethod(_unregister_class_lookup)
  290. def select_related_descend(field, restricted, requested, select_mask, reverse=False):
  291. """
  292. Return True if this field should be used to descend deeper for
  293. select_related() purposes. Used by both the query construction code
  294. (compiler.get_related_selections()) and the model instance creation code
  295. (compiler.klass_info).
  296. Arguments:
  297. * field - the field to be checked
  298. * restricted - a boolean field, indicating if the field list has been
  299. manually restricted using a requested clause)
  300. * requested - The select_related() dictionary.
  301. * select_mask - the dictionary of selected fields.
  302. * reverse - boolean, True if we are checking a reverse select related
  303. """
  304. if not field.remote_field:
  305. return False
  306. if field.remote_field.parent_link and not reverse:
  307. return False
  308. if restricted:
  309. if reverse and field.related_query_name() not in requested:
  310. return False
  311. if not reverse and field.name not in requested:
  312. return False
  313. if not restricted and field.null:
  314. return False
  315. if (
  316. restricted
  317. and select_mask
  318. and field.name in requested
  319. and field not in select_mask
  320. ):
  321. raise FieldError(
  322. f"Field {field.model._meta.object_name}.{field.name} cannot be both "
  323. "deferred and traversed using select_related at the same time."
  324. )
  325. return True
  326. def refs_expression(lookup_parts, annotations):
  327. """
  328. Check if the lookup_parts contains references to the given annotations set.
  329. Because the LOOKUP_SEP is contained in the default annotation names, check
  330. each prefix of the lookup_parts for a match.
  331. """
  332. for n in range(1, len(lookup_parts) + 1):
  333. level_n_lookup = LOOKUP_SEP.join(lookup_parts[0:n])
  334. if annotations.get(level_n_lookup):
  335. return level_n_lookup, lookup_parts[n:]
  336. return None, ()
  337. def check_rel_lookup_compatibility(model, target_opts, field):
  338. """
  339. Check that self.model is compatible with target_opts. Compatibility
  340. is OK if:
  341. 1) model and opts match (where proxy inheritance is removed)
  342. 2) model is parent of opts' model or the other way around
  343. """
  344. def check(opts):
  345. return (
  346. model._meta.concrete_model == opts.concrete_model
  347. or opts.concrete_model in model._meta.get_parent_list()
  348. or model in opts.get_parent_list()
  349. )
  350. # If the field is a primary key, then doing a query against the field's
  351. # model is ok, too. Consider the case:
  352. # class Restaurant(models.Model):
  353. # place = OneToOneField(Place, primary_key=True):
  354. # Restaurant.objects.filter(pk__in=Restaurant.objects.all()).
  355. # If we didn't have the primary key check, then pk__in (== place__in) would
  356. # give Place's opts as the target opts, but Restaurant isn't compatible
  357. # with that. This logic applies only to primary keys, as when doing __in=qs,
  358. # we are going to turn this into __in=qs.values('pk') later on.
  359. return check(target_opts) or (
  360. getattr(field, "primary_key", False) and check(field.model._meta)
  361. )
  362. class FilteredRelation:
  363. """Specify custom filtering in the ON clause of SQL joins."""
  364. def __init__(self, relation_name, *, condition=Q()):
  365. if not relation_name:
  366. raise ValueError("relation_name cannot be empty.")
  367. self.relation_name = relation_name
  368. self.alias = None
  369. if not isinstance(condition, Q):
  370. raise ValueError("condition argument must be a Q() instance.")
  371. # .condition and .resolved_condition have to be stored independently
  372. # as the former must remain unchanged for Join.__eq__ to remain stable
  373. # and reusable even once their .filtered_relation are resolved.
  374. self.condition = condition
  375. self.resolved_condition = None
  376. def __eq__(self, other):
  377. if not isinstance(other, self.__class__):
  378. return NotImplemented
  379. return (
  380. self.relation_name == other.relation_name
  381. and self.alias == other.alias
  382. and self.condition == other.condition
  383. )
  384. def clone(self):
  385. clone = FilteredRelation(self.relation_name, condition=self.condition)
  386. clone.alias = self.alias
  387. if (resolved_condition := self.resolved_condition) is not None:
  388. clone.resolved_condition = resolved_condition.clone()
  389. return clone
  390. def relabeled_clone(self, change_map):
  391. clone = self.clone()
  392. if resolved_condition := clone.resolved_condition:
  393. clone.resolved_condition = resolved_condition.relabeled_clone(change_map)
  394. return clone
  395. def resolve_expression(self, query, reuse, *args, **kwargs):
  396. clone = self.clone()
  397. clone.resolved_condition = query.build_filter(
  398. self.condition,
  399. can_reuse=reuse,
  400. allow_joins=True,
  401. split_subq=False,
  402. update_join_types=False,
  403. )[0]
  404. return clone
  405. def as_sql(self, compiler, connection):
  406. return compiler.compile(self.resolved_condition)