query_utils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. from __future__ import unicode_literals
  8. from django.apps import apps
  9. from django.db.backends import utils
  10. from django.utils import six
  11. from django.utils import tree
  12. class InvalidQuery(Exception):
  13. """
  14. The query passed to raw isn't a safe query to use with raw.
  15. """
  16. pass
  17. class QueryWrapper(object):
  18. """
  19. A type that indicates the contents are an SQL fragment and the associate
  20. parameters. Can be used to pass opaque data to a where-clause, for example.
  21. """
  22. def __init__(self, sql, params):
  23. self.data = sql, list(params)
  24. def as_sql(self, qn=None, connection=None):
  25. return self.data
  26. class Q(tree.Node):
  27. """
  28. Encapsulates filters as objects that can then be combined logically (using
  29. & and |).
  30. """
  31. # Connection types
  32. AND = 'AND'
  33. OR = 'OR'
  34. default = AND
  35. def __init__(self, *args, **kwargs):
  36. super(Q, self).__init__(children=list(args) + list(six.iteritems(kwargs)))
  37. def _combine(self, other, conn):
  38. if not isinstance(other, Q):
  39. raise TypeError(other)
  40. obj = type(self)()
  41. obj.connector = conn
  42. obj.add(self, conn)
  43. obj.add(other, conn)
  44. return obj
  45. def __or__(self, other):
  46. return self._combine(other, self.OR)
  47. def __and__(self, other):
  48. return self._combine(other, self.AND)
  49. def __invert__(self):
  50. obj = type(self)()
  51. obj.add(self, self.AND)
  52. obj.negate()
  53. return obj
  54. def clone(self):
  55. clone = self.__class__._new_instance(
  56. children=[], connector=self.connector, negated=self.negated)
  57. for child in self.children:
  58. if hasattr(child, 'clone'):
  59. clone.children.append(child.clone())
  60. else:
  61. clone.children.append(child)
  62. return clone
  63. class DeferredAttribute(object):
  64. """
  65. A wrapper for a deferred-loading field. When the value is read from this
  66. object the first time, the query is executed.
  67. """
  68. def __init__(self, field_name, model):
  69. self.field_name = field_name
  70. def __get__(self, instance, owner):
  71. """
  72. Retrieves and caches the value from the datastore on the first lookup.
  73. Returns the cached value.
  74. """
  75. from django.db.models.fields import FieldDoesNotExist
  76. non_deferred_model = instance._meta.proxy_for_model
  77. opts = non_deferred_model._meta
  78. assert instance is not None
  79. data = instance.__dict__
  80. if data.get(self.field_name, self) is self:
  81. # self.field_name is the attname of the field, but only() takes the
  82. # actual name, so we need to translate it here.
  83. try:
  84. f = opts.get_field_by_name(self.field_name)[0]
  85. except FieldDoesNotExist:
  86. f = [f for f in opts.fields if f.attname == self.field_name][0]
  87. name = f.name
  88. # Let's see if the field is part of the parent chain. If so we
  89. # might be able to reuse the already loaded value. Refs #18343.
  90. val = self._check_parent_chain(instance, name)
  91. if val is None:
  92. # We use only() instead of values() here because we want the
  93. # various data coercion methods (to_python(), etc.) to be
  94. # called here.
  95. val = getattr(
  96. non_deferred_model._base_manager.only(name).using(
  97. instance._state.db).get(pk=instance.pk),
  98. self.field_name
  99. )
  100. data[self.field_name] = val
  101. return data[self.field_name]
  102. def __set__(self, instance, value):
  103. """
  104. Deferred loading attributes can be set normally (which means there will
  105. never be a database lookup involved.
  106. """
  107. instance.__dict__[self.field_name] = value
  108. def _check_parent_chain(self, instance, name):
  109. """
  110. Check if the field value can be fetched from a parent field already
  111. loaded in the instance. This can be done if the to-be fetched
  112. field is a primary key field.
  113. """
  114. opts = instance._meta
  115. f = opts.get_field_by_name(name)[0]
  116. link_field = opts.get_ancestor_link(f.model)
  117. if f.primary_key and f != link_field:
  118. return getattr(instance, link_field.attname)
  119. return None
  120. def select_related_descend(field, restricted, requested, load_fields, reverse=False):
  121. """
  122. Returns True if this field should be used to descend deeper for
  123. select_related() purposes. Used by both the query construction code
  124. (sql.query.fill_related_selections()) and the model instance creation code
  125. (query.get_klass_info()).
  126. Arguments:
  127. * field - the field to be checked
  128. * restricted - a boolean field, indicating if the field list has been
  129. manually restricted using a requested clause)
  130. * requested - The select_related() dictionary.
  131. * load_fields - the set of fields to be loaded on this model
  132. * reverse - boolean, True if we are checking a reverse select related
  133. """
  134. if not field.rel:
  135. return False
  136. if field.rel.parent_link and not reverse:
  137. return False
  138. if restricted:
  139. if reverse and field.related_query_name() not in requested:
  140. return False
  141. if not reverse and field.name not in requested:
  142. return False
  143. if not restricted and field.null:
  144. return False
  145. if load_fields:
  146. if field.name not in load_fields:
  147. if restricted and field.name in requested:
  148. raise InvalidQuery("Field %s.%s cannot be both deferred"
  149. " and traversed using select_related"
  150. " at the same time." %
  151. (field.model._meta.object_name, field.name))
  152. return False
  153. return True
  154. # This function is needed because data descriptors must be defined on a class
  155. # object, not an instance, to have any effect.
  156. def deferred_class_factory(model, attrs):
  157. """
  158. Returns a class object that is a copy of "model" with the specified "attrs"
  159. being replaced with DeferredAttribute objects. The "pk_value" ties the
  160. deferred attributes to a particular instance of the model.
  161. """
  162. # The app registry wants a unique name for each model, otherwise the new
  163. # class won't be created (we get an exception). Therefore, we generate
  164. # the name using the passed in attrs. It's OK to reuse an existing class
  165. # object if the attrs are identical.
  166. name = "%s_Deferred_%s" % (model.__name__, '_'.join(sorted(list(attrs))))
  167. name = utils.truncate_name(name, 80, 32)
  168. try:
  169. return apps.get_model(model._meta.app_label, name)
  170. except LookupError:
  171. class Meta:
  172. proxy = True
  173. app_label = model._meta.app_label
  174. overrides = dict((attr, DeferredAttribute(attr, model)) for attr in attrs)
  175. overrides["Meta"] = Meta
  176. overrides["__module__"] = model.__module__
  177. overrides["_deferred"] = True
  178. return type(str(name), (model,), overrides)
  179. # The above function is also used to unpickle model instances with deferred
  180. # fields.
  181. deferred_class_factory.__safe_for_unpickling__ = True