base.py 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  1. import copy
  2. import inspect
  3. import warnings
  4. from functools import partialmethod
  5. from itertools import chain
  6. from asgiref.sync import sync_to_async
  7. import django
  8. from django.apps import apps
  9. from django.conf import settings
  10. from django.core import checks
  11. from django.core.exceptions import (
  12. NON_FIELD_ERRORS,
  13. FieldDoesNotExist,
  14. FieldError,
  15. MultipleObjectsReturned,
  16. ObjectDoesNotExist,
  17. ValidationError,
  18. )
  19. from django.db import (
  20. DJANGO_VERSION_PICKLE_KEY,
  21. DatabaseError,
  22. connection,
  23. connections,
  24. router,
  25. transaction,
  26. )
  27. from django.db.models import NOT_PROVIDED, ExpressionWrapper, IntegerField, Max, Value
  28. from django.db.models.constants import LOOKUP_SEP
  29. from django.db.models.constraints import CheckConstraint, UniqueConstraint
  30. from django.db.models.deletion import CASCADE, Collector
  31. from django.db.models.expressions import RawSQL
  32. from django.db.models.fields.related import (
  33. ForeignObjectRel,
  34. OneToOneField,
  35. lazy_related_operation,
  36. resolve_relation,
  37. )
  38. from django.db.models.functions import Coalesce
  39. from django.db.models.manager import Manager
  40. from django.db.models.options import Options
  41. from django.db.models.query import F, Q
  42. from django.db.models.signals import (
  43. class_prepared,
  44. post_init,
  45. post_save,
  46. pre_init,
  47. pre_save,
  48. )
  49. from django.db.models.utils import AltersData, make_model_tuple
  50. from django.utils.encoding import force_str
  51. from django.utils.hashable import make_hashable
  52. from django.utils.text import capfirst, get_text_list
  53. from django.utils.translation import gettext_lazy as _
  54. class Deferred:
  55. def __repr__(self):
  56. return "<Deferred field>"
  57. def __str__(self):
  58. return "<Deferred field>"
  59. DEFERRED = Deferred()
  60. def subclass_exception(name, bases, module, attached_to):
  61. """
  62. Create exception subclass. Used by ModelBase below.
  63. The exception is created in a way that allows it to be pickled, assuming
  64. that the returned exception class will be added as an attribute to the
  65. 'attached_to' class.
  66. """
  67. return type(
  68. name,
  69. bases,
  70. {
  71. "__module__": module,
  72. "__qualname__": "%s.%s" % (attached_to.__qualname__, name),
  73. },
  74. )
  75. def _has_contribute_to_class(value):
  76. # Only call contribute_to_class() if it's bound.
  77. return not inspect.isclass(value) and hasattr(value, "contribute_to_class")
  78. class ModelBase(type):
  79. """Metaclass for all models."""
  80. def __new__(cls, name, bases, attrs, **kwargs):
  81. super_new = super().__new__
  82. # Also ensure initialization is only performed for subclasses of Model
  83. # (excluding Model class itself).
  84. parents = [b for b in bases if isinstance(b, ModelBase)]
  85. if not parents:
  86. return super_new(cls, name, bases, attrs)
  87. # Create the class.
  88. module = attrs.pop("__module__")
  89. new_attrs = {"__module__": module}
  90. classcell = attrs.pop("__classcell__", None)
  91. if classcell is not None:
  92. new_attrs["__classcell__"] = classcell
  93. attr_meta = attrs.pop("Meta", None)
  94. # Pass all attrs without a (Django-specific) contribute_to_class()
  95. # method to type.__new__() so that they're properly initialized
  96. # (i.e. __set_name__()).
  97. contributable_attrs = {}
  98. for obj_name, obj in attrs.items():
  99. if _has_contribute_to_class(obj):
  100. contributable_attrs[obj_name] = obj
  101. else:
  102. new_attrs[obj_name] = obj
  103. new_class = super_new(cls, name, bases, new_attrs, **kwargs)
  104. abstract = getattr(attr_meta, "abstract", False)
  105. meta = attr_meta or getattr(new_class, "Meta", None)
  106. base_meta = getattr(new_class, "_meta", None)
  107. app_label = None
  108. # Look for an application configuration to attach the model to.
  109. app_config = apps.get_containing_app_config(module)
  110. if getattr(meta, "app_label", None) is None:
  111. if app_config is None:
  112. if not abstract:
  113. raise RuntimeError(
  114. "Model class %s.%s doesn't declare an explicit "
  115. "app_label and isn't in an application in "
  116. "INSTALLED_APPS." % (module, name)
  117. )
  118. else:
  119. app_label = app_config.label
  120. new_class.add_to_class("_meta", Options(meta, app_label))
  121. if not abstract:
  122. new_class.add_to_class(
  123. "DoesNotExist",
  124. subclass_exception(
  125. "DoesNotExist",
  126. tuple(
  127. x.DoesNotExist
  128. for x in parents
  129. if hasattr(x, "_meta") and not x._meta.abstract
  130. )
  131. or (ObjectDoesNotExist,),
  132. module,
  133. attached_to=new_class,
  134. ),
  135. )
  136. new_class.add_to_class(
  137. "MultipleObjectsReturned",
  138. subclass_exception(
  139. "MultipleObjectsReturned",
  140. tuple(
  141. x.MultipleObjectsReturned
  142. for x in parents
  143. if hasattr(x, "_meta") and not x._meta.abstract
  144. )
  145. or (MultipleObjectsReturned,),
  146. module,
  147. attached_to=new_class,
  148. ),
  149. )
  150. if base_meta and not base_meta.abstract:
  151. # Non-abstract child classes inherit some attributes from their
  152. # non-abstract parent (unless an ABC comes before it in the
  153. # method resolution order).
  154. if not hasattr(meta, "ordering"):
  155. new_class._meta.ordering = base_meta.ordering
  156. if not hasattr(meta, "get_latest_by"):
  157. new_class._meta.get_latest_by = base_meta.get_latest_by
  158. is_proxy = new_class._meta.proxy
  159. # If the model is a proxy, ensure that the base class
  160. # hasn't been swapped out.
  161. if is_proxy and base_meta and base_meta.swapped:
  162. raise TypeError(
  163. "%s cannot proxy the swapped model '%s'." % (name, base_meta.swapped)
  164. )
  165. # Add remaining attributes (those with a contribute_to_class() method)
  166. # to the class.
  167. for obj_name, obj in contributable_attrs.items():
  168. new_class.add_to_class(obj_name, obj)
  169. # All the fields of any type declared on this model
  170. new_fields = chain(
  171. new_class._meta.local_fields,
  172. new_class._meta.local_many_to_many,
  173. new_class._meta.private_fields,
  174. )
  175. field_names = {f.name for f in new_fields}
  176. # Basic setup for proxy models.
  177. if is_proxy:
  178. base = None
  179. for parent in [kls for kls in parents if hasattr(kls, "_meta")]:
  180. if parent._meta.abstract:
  181. if parent._meta.fields:
  182. raise TypeError(
  183. "Abstract base class containing model fields not "
  184. "permitted for proxy model '%s'." % name
  185. )
  186. else:
  187. continue
  188. if base is None:
  189. base = parent
  190. elif parent._meta.concrete_model is not base._meta.concrete_model:
  191. raise TypeError(
  192. "Proxy model '%s' has more than one non-abstract model base "
  193. "class." % name
  194. )
  195. if base is None:
  196. raise TypeError(
  197. "Proxy model '%s' has no non-abstract model base class." % name
  198. )
  199. new_class._meta.setup_proxy(base)
  200. new_class._meta.concrete_model = base._meta.concrete_model
  201. else:
  202. new_class._meta.concrete_model = new_class
  203. # Collect the parent links for multi-table inheritance.
  204. parent_links = {}
  205. for base in reversed([new_class] + parents):
  206. # Conceptually equivalent to `if base is Model`.
  207. if not hasattr(base, "_meta"):
  208. continue
  209. # Skip concrete parent classes.
  210. if base != new_class and not base._meta.abstract:
  211. continue
  212. # Locate OneToOneField instances.
  213. for field in base._meta.local_fields:
  214. if isinstance(field, OneToOneField) and field.remote_field.parent_link:
  215. related = resolve_relation(new_class, field.remote_field.model)
  216. parent_links[make_model_tuple(related)] = field
  217. # Track fields inherited from base models.
  218. inherited_attributes = set()
  219. # Do the appropriate setup for any model parents.
  220. for base in new_class.mro():
  221. if base not in parents or not hasattr(base, "_meta"):
  222. # Things without _meta aren't functional models, so they're
  223. # uninteresting parents.
  224. inherited_attributes.update(base.__dict__)
  225. continue
  226. parent_fields = base._meta.local_fields + base._meta.local_many_to_many
  227. if not base._meta.abstract:
  228. # Check for clashes between locally declared fields and those
  229. # on the base classes.
  230. for field in parent_fields:
  231. if field.name in field_names:
  232. raise FieldError(
  233. "Local field %r in class %r clashes with field of "
  234. "the same name from base class %r."
  235. % (
  236. field.name,
  237. name,
  238. base.__name__,
  239. )
  240. )
  241. else:
  242. inherited_attributes.add(field.name)
  243. # Concrete classes...
  244. base = base._meta.concrete_model
  245. base_key = make_model_tuple(base)
  246. if base_key in parent_links:
  247. field = parent_links[base_key]
  248. elif not is_proxy:
  249. attr_name = "%s_ptr" % base._meta.model_name
  250. field = OneToOneField(
  251. base,
  252. on_delete=CASCADE,
  253. name=attr_name,
  254. auto_created=True,
  255. parent_link=True,
  256. )
  257. if attr_name in field_names:
  258. raise FieldError(
  259. "Auto-generated field '%s' in class %r for "
  260. "parent_link to base class %r clashes with "
  261. "declared field of the same name."
  262. % (
  263. attr_name,
  264. name,
  265. base.__name__,
  266. )
  267. )
  268. # Only add the ptr field if it's not already present;
  269. # e.g. migrations will already have it specified
  270. if not hasattr(new_class, attr_name):
  271. new_class.add_to_class(attr_name, field)
  272. else:
  273. field = None
  274. new_class._meta.parents[base] = field
  275. else:
  276. base_parents = base._meta.parents.copy()
  277. # Add fields from abstract base class if it wasn't overridden.
  278. for field in parent_fields:
  279. if (
  280. field.name not in field_names
  281. and field.name not in new_class.__dict__
  282. and field.name not in inherited_attributes
  283. ):
  284. new_field = copy.deepcopy(field)
  285. new_class.add_to_class(field.name, new_field)
  286. # Replace parent links defined on this base by the new
  287. # field. It will be appropriately resolved if required.
  288. if field.one_to_one:
  289. for parent, parent_link in base_parents.items():
  290. if field == parent_link:
  291. base_parents[parent] = new_field
  292. # Pass any non-abstract parent classes onto child.
  293. new_class._meta.parents.update(base_parents)
  294. # Inherit private fields (like GenericForeignKey) from the parent
  295. # class
  296. for field in base._meta.private_fields:
  297. if field.name in field_names:
  298. if not base._meta.abstract:
  299. raise FieldError(
  300. "Local field %r in class %r clashes with field of "
  301. "the same name from base class %r."
  302. % (
  303. field.name,
  304. name,
  305. base.__name__,
  306. )
  307. )
  308. else:
  309. field = copy.deepcopy(field)
  310. if not base._meta.abstract:
  311. field.mti_inherited = True
  312. new_class.add_to_class(field.name, field)
  313. # Copy indexes so that index names are unique when models extend an
  314. # abstract model.
  315. new_class._meta.indexes = [
  316. copy.deepcopy(idx) for idx in new_class._meta.indexes
  317. ]
  318. if abstract:
  319. # Abstract base models can't be instantiated and don't appear in
  320. # the list of models for an app. We do the final setup for them a
  321. # little differently from normal models.
  322. attr_meta.abstract = False
  323. new_class.Meta = attr_meta
  324. return new_class
  325. new_class._prepare()
  326. new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  327. return new_class
  328. def add_to_class(cls, name, value):
  329. if _has_contribute_to_class(value):
  330. value.contribute_to_class(cls, name)
  331. else:
  332. setattr(cls, name, value)
  333. def _prepare(cls):
  334. """Create some methods once self._meta has been populated."""
  335. opts = cls._meta
  336. opts._prepare(cls)
  337. if opts.order_with_respect_to:
  338. cls.get_next_in_order = partialmethod(
  339. cls._get_next_or_previous_in_order, is_next=True
  340. )
  341. cls.get_previous_in_order = partialmethod(
  342. cls._get_next_or_previous_in_order, is_next=False
  343. )
  344. # Defer creating accessors on the foreign class until it has been
  345. # created and registered. If remote_field is None, we're ordering
  346. # with respect to a GenericForeignKey and don't know what the
  347. # foreign class is - we'll add those accessors later in
  348. # contribute_to_class().
  349. if opts.order_with_respect_to.remote_field:
  350. wrt = opts.order_with_respect_to
  351. remote = wrt.remote_field.model
  352. lazy_related_operation(make_foreign_order_accessors, cls, remote)
  353. # Give the class a docstring -- its definition.
  354. if cls.__doc__ is None:
  355. cls.__doc__ = "%s(%s)" % (
  356. cls.__name__,
  357. ", ".join(f.name for f in opts.fields),
  358. )
  359. get_absolute_url_override = settings.ABSOLUTE_URL_OVERRIDES.get(
  360. opts.label_lower
  361. )
  362. if get_absolute_url_override:
  363. setattr(cls, "get_absolute_url", get_absolute_url_override)
  364. if not opts.managers:
  365. if any(f.name == "objects" for f in opts.fields):
  366. raise ValueError(
  367. "Model %s must specify a custom Manager, because it has a "
  368. "field named 'objects'." % cls.__name__
  369. )
  370. manager = Manager()
  371. manager.auto_created = True
  372. cls.add_to_class("objects", manager)
  373. # Set the name of _meta.indexes. This can't be done in
  374. # Options.contribute_to_class() because fields haven't been added to
  375. # the model at that point.
  376. for index in cls._meta.indexes:
  377. if not index.name:
  378. index.set_name_with_model(cls)
  379. class_prepared.send(sender=cls)
  380. @property
  381. def _base_manager(cls):
  382. return cls._meta.base_manager
  383. @property
  384. def _default_manager(cls):
  385. return cls._meta.default_manager
  386. class ModelStateFieldsCacheDescriptor:
  387. def __get__(self, instance, cls=None):
  388. if instance is None:
  389. return self
  390. res = instance.fields_cache = {}
  391. return res
  392. class ModelState:
  393. """Store model instance state."""
  394. db = None
  395. # If true, uniqueness validation checks will consider this a new, unsaved
  396. # object. Necessary for correct validation of new instances of objects with
  397. # explicit (non-auto) PKs. This impacts validation only; it has no effect
  398. # on the actual save.
  399. adding = True
  400. fields_cache = ModelStateFieldsCacheDescriptor()
  401. class Model(AltersData, metaclass=ModelBase):
  402. def __init__(self, *args, **kwargs):
  403. # Alias some things as locals to avoid repeat global lookups
  404. cls = self.__class__
  405. opts = self._meta
  406. _setattr = setattr
  407. _DEFERRED = DEFERRED
  408. if opts.abstract:
  409. raise TypeError("Abstract models cannot be instantiated.")
  410. pre_init.send(sender=cls, args=args, kwargs=kwargs)
  411. # Set up the storage for instance state
  412. self._state = ModelState()
  413. # There is a rather weird disparity here; if kwargs, it's set, then args
  414. # overrides it. It should be one or the other; don't duplicate the work
  415. # The reason for the kwargs check is that standard iterator passes in by
  416. # args, and instantiation for iteration is 33% faster.
  417. if len(args) > len(opts.concrete_fields):
  418. # Daft, but matches old exception sans the err msg.
  419. raise IndexError("Number of args exceeds number of fields")
  420. if not kwargs:
  421. fields_iter = iter(opts.concrete_fields)
  422. # The ordering of the zip calls matter - zip throws StopIteration
  423. # when an iter throws it. So if the first iter throws it, the second
  424. # is *not* consumed. We rely on this, so don't change the order
  425. # without changing the logic.
  426. for val, field in zip(args, fields_iter):
  427. if val is _DEFERRED:
  428. continue
  429. _setattr(self, field.attname, val)
  430. else:
  431. # Slower, kwargs-ready version.
  432. fields_iter = iter(opts.fields)
  433. for val, field in zip(args, fields_iter):
  434. if val is _DEFERRED:
  435. continue
  436. _setattr(self, field.attname, val)
  437. if kwargs.pop(field.name, NOT_PROVIDED) is not NOT_PROVIDED:
  438. raise TypeError(
  439. f"{cls.__qualname__}() got both positional and "
  440. f"keyword arguments for field '{field.name}'."
  441. )
  442. # Now we're left with the unprocessed fields that *must* come from
  443. # keywords, or default.
  444. for field in fields_iter:
  445. is_related_object = False
  446. # Virtual field
  447. if field.attname not in kwargs and field.column is None or field.generated:
  448. continue
  449. if kwargs:
  450. if isinstance(field.remote_field, ForeignObjectRel):
  451. try:
  452. # Assume object instance was passed in.
  453. rel_obj = kwargs.pop(field.name)
  454. is_related_object = True
  455. except KeyError:
  456. try:
  457. # Object instance wasn't passed in -- must be an ID.
  458. val = kwargs.pop(field.attname)
  459. except KeyError:
  460. val = field.get_default()
  461. else:
  462. try:
  463. val = kwargs.pop(field.attname)
  464. except KeyError:
  465. # This is done with an exception rather than the
  466. # default argument on pop because we don't want
  467. # get_default() to be evaluated, and then not used.
  468. # Refs #12057.
  469. val = field.get_default()
  470. else:
  471. val = field.get_default()
  472. if is_related_object:
  473. # If we are passed a related instance, set it using the
  474. # field.name instead of field.attname (e.g. "user" instead of
  475. # "user_id") so that the object gets properly cached (and type
  476. # checked) by the RelatedObjectDescriptor.
  477. if rel_obj is not _DEFERRED:
  478. _setattr(self, field.name, rel_obj)
  479. else:
  480. if val is not _DEFERRED:
  481. _setattr(self, field.attname, val)
  482. if kwargs:
  483. property_names = opts._property_names
  484. unexpected = ()
  485. for prop, value in kwargs.items():
  486. # Any remaining kwargs must correspond to properties or virtual
  487. # fields.
  488. if prop in property_names:
  489. if value is not _DEFERRED:
  490. _setattr(self, prop, value)
  491. else:
  492. try:
  493. opts.get_field(prop)
  494. except FieldDoesNotExist:
  495. unexpected += (prop,)
  496. else:
  497. if value is not _DEFERRED:
  498. _setattr(self, prop, value)
  499. if unexpected:
  500. unexpected_names = ", ".join(repr(n) for n in unexpected)
  501. raise TypeError(
  502. f"{cls.__name__}() got unexpected keyword arguments: "
  503. f"{unexpected_names}"
  504. )
  505. super().__init__()
  506. post_init.send(sender=cls, instance=self)
  507. @classmethod
  508. def from_db(cls, db, field_names, values):
  509. if len(values) != len(cls._meta.concrete_fields):
  510. values_iter = iter(values)
  511. values = [
  512. next(values_iter) if f.attname in field_names else DEFERRED
  513. for f in cls._meta.concrete_fields
  514. ]
  515. new = cls(*values)
  516. new._state.adding = False
  517. new._state.db = db
  518. return new
  519. def __repr__(self):
  520. return "<%s: %s>" % (self.__class__.__name__, self)
  521. def __str__(self):
  522. return "%s object (%s)" % (self.__class__.__name__, self.pk)
  523. def __eq__(self, other):
  524. if not isinstance(other, Model):
  525. return NotImplemented
  526. if self._meta.concrete_model != other._meta.concrete_model:
  527. return False
  528. my_pk = self.pk
  529. if my_pk is None:
  530. return self is other
  531. return my_pk == other.pk
  532. def __hash__(self):
  533. if self.pk is None:
  534. raise TypeError("Model instances without primary key value are unhashable")
  535. return hash(self.pk)
  536. def __reduce__(self):
  537. data = self.__getstate__()
  538. data[DJANGO_VERSION_PICKLE_KEY] = django.__version__
  539. class_id = self._meta.app_label, self._meta.object_name
  540. return model_unpickle, (class_id,), data
  541. def __getstate__(self):
  542. """Hook to allow choosing the attributes to pickle."""
  543. state = self.__dict__.copy()
  544. state["_state"] = copy.copy(state["_state"])
  545. state["_state"].fields_cache = state["_state"].fields_cache.copy()
  546. # memoryview cannot be pickled, so cast it to bytes and store
  547. # separately.
  548. _memoryview_attrs = []
  549. for attr, value in state.items():
  550. if isinstance(value, memoryview):
  551. _memoryview_attrs.append((attr, bytes(value)))
  552. if _memoryview_attrs:
  553. state["_memoryview_attrs"] = _memoryview_attrs
  554. for attr, value in _memoryview_attrs:
  555. state.pop(attr)
  556. return state
  557. def __setstate__(self, state):
  558. pickled_version = state.get(DJANGO_VERSION_PICKLE_KEY)
  559. if pickled_version:
  560. if pickled_version != django.__version__:
  561. warnings.warn(
  562. "Pickled model instance's Django version %s does not "
  563. "match the current version %s."
  564. % (pickled_version, django.__version__),
  565. RuntimeWarning,
  566. stacklevel=2,
  567. )
  568. else:
  569. warnings.warn(
  570. "Pickled model instance's Django version is not specified.",
  571. RuntimeWarning,
  572. stacklevel=2,
  573. )
  574. if "_memoryview_attrs" in state:
  575. for attr, value in state.pop("_memoryview_attrs"):
  576. state[attr] = memoryview(value)
  577. self.__dict__.update(state)
  578. def _get_pk_val(self, meta=None):
  579. meta = meta or self._meta
  580. return getattr(self, meta.pk.attname)
  581. def _set_pk_val(self, value):
  582. for parent_link in self._meta.parents.values():
  583. if parent_link and parent_link != self._meta.pk:
  584. setattr(self, parent_link.target_field.attname, value)
  585. return setattr(self, self._meta.pk.attname, value)
  586. pk = property(_get_pk_val, _set_pk_val)
  587. def get_deferred_fields(self):
  588. """
  589. Return a set containing names of deferred fields for this instance.
  590. """
  591. return {
  592. f.attname
  593. for f in self._meta.concrete_fields
  594. if f.attname not in self.__dict__
  595. }
  596. def refresh_from_db(self, using=None, fields=None):
  597. """
  598. Reload field values from the database.
  599. By default, the reloading happens from the database this instance was
  600. loaded from, or by the read router if this instance wasn't loaded from
  601. any database. The using parameter will override the default.
  602. Fields can be used to specify which fields to reload. The fields
  603. should be an iterable of field attnames. If fields is None, then
  604. all non-deferred fields are reloaded.
  605. When accessing deferred fields of an instance, the deferred loading
  606. of the field will call this method.
  607. """
  608. if fields is None:
  609. self._prefetched_objects_cache = {}
  610. else:
  611. prefetched_objects_cache = getattr(self, "_prefetched_objects_cache", ())
  612. for field in fields:
  613. if field in prefetched_objects_cache:
  614. del prefetched_objects_cache[field]
  615. fields.remove(field)
  616. if not fields:
  617. return
  618. if any(LOOKUP_SEP in f for f in fields):
  619. raise ValueError(
  620. 'Found "%s" in fields argument. Relations and transforms '
  621. "are not allowed in fields." % LOOKUP_SEP
  622. )
  623. hints = {"instance": self}
  624. db_instance_qs = self.__class__._base_manager.db_manager(
  625. using, hints=hints
  626. ).filter(pk=self.pk)
  627. # Use provided fields, if not set then reload all non-deferred fields.
  628. deferred_fields = self.get_deferred_fields()
  629. if fields is not None:
  630. fields = list(fields)
  631. db_instance_qs = db_instance_qs.only(*fields)
  632. elif deferred_fields:
  633. fields = [
  634. f.attname
  635. for f in self._meta.concrete_fields
  636. if f.attname not in deferred_fields
  637. ]
  638. db_instance_qs = db_instance_qs.only(*fields)
  639. db_instance = db_instance_qs.get()
  640. non_loaded_fields = db_instance.get_deferred_fields()
  641. for field in self._meta.concrete_fields:
  642. if field.attname in non_loaded_fields:
  643. # This field wasn't refreshed - skip ahead.
  644. continue
  645. setattr(self, field.attname, getattr(db_instance, field.attname))
  646. # Clear cached foreign keys.
  647. if field.is_relation and field.is_cached(self):
  648. field.delete_cached_value(self)
  649. # Clear cached relations.
  650. for field in self._meta.related_objects:
  651. if field.is_cached(self):
  652. field.delete_cached_value(self)
  653. # Clear cached private relations.
  654. for field in self._meta.private_fields:
  655. if field.is_relation and field.is_cached(self):
  656. field.delete_cached_value(self)
  657. self._state.db = db_instance._state.db
  658. async def arefresh_from_db(self, using=None, fields=None):
  659. return await sync_to_async(self.refresh_from_db)(using=using, fields=fields)
  660. def serializable_value(self, field_name):
  661. """
  662. Return the value of the field name for this instance. If the field is
  663. a foreign key, return the id value instead of the object. If there's
  664. no Field object with this name on the model, return the model
  665. attribute's value.
  666. Used to serialize a field's value (in the serializer, or form output,
  667. for example). Normally, you would just access the attribute directly
  668. and not use this method.
  669. """
  670. try:
  671. field = self._meta.get_field(field_name)
  672. except FieldDoesNotExist:
  673. return getattr(self, field_name)
  674. return getattr(self, field.attname)
  675. def save(
  676. self, force_insert=False, force_update=False, using=None, update_fields=None
  677. ):
  678. """
  679. Save the current instance. Override this in a subclass if you want to
  680. control the saving process.
  681. The 'force_insert' and 'force_update' parameters can be used to insist
  682. that the "save" must be an SQL insert or update (or equivalent for
  683. non-SQL backends), respectively. Normally, they should not be set.
  684. """
  685. self._prepare_related_fields_for_save(operation_name="save")
  686. using = using or router.db_for_write(self.__class__, instance=self)
  687. if force_insert and (force_update or update_fields):
  688. raise ValueError("Cannot force both insert and updating in model saving.")
  689. deferred_fields = self.get_deferred_fields()
  690. if update_fields is not None:
  691. # If update_fields is empty, skip the save. We do also check for
  692. # no-op saves later on for inheritance cases. This bailout is
  693. # still needed for skipping signal sending.
  694. if not update_fields:
  695. return
  696. update_fields = frozenset(update_fields)
  697. field_names = self._meta._non_pk_concrete_field_names
  698. non_model_fields = update_fields.difference(field_names)
  699. if non_model_fields:
  700. raise ValueError(
  701. "The following fields do not exist in this model, are m2m "
  702. "fields, or are non-concrete fields: %s"
  703. % ", ".join(non_model_fields)
  704. )
  705. # If saving to the same database, and this model is deferred, then
  706. # automatically do an "update_fields" save on the loaded fields.
  707. elif not force_insert and deferred_fields and using == self._state.db:
  708. field_names = set()
  709. for field in self._meta.concrete_fields:
  710. if not field.primary_key and not hasattr(field, "through"):
  711. field_names.add(field.attname)
  712. loaded_fields = field_names.difference(deferred_fields)
  713. if loaded_fields:
  714. update_fields = frozenset(loaded_fields)
  715. self.save_base(
  716. using=using,
  717. force_insert=force_insert,
  718. force_update=force_update,
  719. update_fields=update_fields,
  720. )
  721. save.alters_data = True
  722. async def asave(
  723. self, force_insert=False, force_update=False, using=None, update_fields=None
  724. ):
  725. return await sync_to_async(self.save)(
  726. force_insert=force_insert,
  727. force_update=force_update,
  728. using=using,
  729. update_fields=update_fields,
  730. )
  731. asave.alters_data = True
  732. @classmethod
  733. def _validate_force_insert(cls, force_insert):
  734. if force_insert is False:
  735. return ()
  736. if force_insert is True:
  737. return (cls,)
  738. if not isinstance(force_insert, tuple):
  739. raise TypeError("force_insert must be a bool or tuple.")
  740. for member in force_insert:
  741. if not isinstance(member, ModelBase):
  742. raise TypeError(
  743. f"Invalid force_insert member. {member!r} must be a model subclass."
  744. )
  745. if not issubclass(cls, member):
  746. raise TypeError(
  747. f"Invalid force_insert member. {member.__qualname__} must be a "
  748. f"base of {cls.__qualname__}."
  749. )
  750. return force_insert
  751. def save_base(
  752. self,
  753. raw=False,
  754. force_insert=False,
  755. force_update=False,
  756. using=None,
  757. update_fields=None,
  758. ):
  759. """
  760. Handle the parts of saving which should be done only once per save,
  761. yet need to be done in raw saves, too. This includes some sanity
  762. checks and signal sending.
  763. The 'raw' argument is telling save_base not to save any parent
  764. models and not to do any changes to the values before save. This
  765. is used by fixture loading.
  766. """
  767. using = using or router.db_for_write(self.__class__, instance=self)
  768. assert not (force_insert and (force_update or update_fields))
  769. assert update_fields is None or update_fields
  770. cls = origin = self.__class__
  771. # Skip proxies, but keep the origin as the proxy model.
  772. if cls._meta.proxy:
  773. cls = cls._meta.concrete_model
  774. meta = cls._meta
  775. if not meta.auto_created:
  776. pre_save.send(
  777. sender=origin,
  778. instance=self,
  779. raw=raw,
  780. using=using,
  781. update_fields=update_fields,
  782. )
  783. # A transaction isn't needed if one query is issued.
  784. if meta.parents:
  785. context_manager = transaction.atomic(using=using, savepoint=False)
  786. else:
  787. context_manager = transaction.mark_for_rollback_on_error(using=using)
  788. with context_manager:
  789. parent_inserted = False
  790. if not raw:
  791. # Validate force insert only when parents are inserted.
  792. force_insert = self._validate_force_insert(force_insert)
  793. parent_inserted = self._save_parents(
  794. cls, using, update_fields, force_insert
  795. )
  796. updated = self._save_table(
  797. raw,
  798. cls,
  799. force_insert or parent_inserted,
  800. force_update,
  801. using,
  802. update_fields,
  803. )
  804. # Store the database on which the object was saved
  805. self._state.db = using
  806. # Once saved, this is no longer a to-be-added instance.
  807. self._state.adding = False
  808. # Signal that the save is complete
  809. if not meta.auto_created:
  810. post_save.send(
  811. sender=origin,
  812. instance=self,
  813. created=(not updated),
  814. update_fields=update_fields,
  815. raw=raw,
  816. using=using,
  817. )
  818. save_base.alters_data = True
  819. def _save_parents(
  820. self, cls, using, update_fields, force_insert, updated_parents=None
  821. ):
  822. """Save all the parents of cls using values from self."""
  823. meta = cls._meta
  824. inserted = False
  825. if updated_parents is None:
  826. updated_parents = {}
  827. for parent, field in meta.parents.items():
  828. # Make sure the link fields are synced between parent and self.
  829. if (
  830. field
  831. and getattr(self, parent._meta.pk.attname) is None
  832. and getattr(self, field.attname) is not None
  833. ):
  834. setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
  835. if (parent_updated := updated_parents.get(parent)) is None:
  836. parent_inserted = self._save_parents(
  837. cls=parent,
  838. using=using,
  839. update_fields=update_fields,
  840. force_insert=force_insert,
  841. updated_parents=updated_parents,
  842. )
  843. updated = self._save_table(
  844. cls=parent,
  845. using=using,
  846. update_fields=update_fields,
  847. force_insert=parent_inserted or issubclass(parent, force_insert),
  848. )
  849. if not updated:
  850. inserted = True
  851. updated_parents[parent] = updated
  852. elif not parent_updated:
  853. inserted = True
  854. # Set the parent's PK value to self.
  855. if field:
  856. setattr(self, field.attname, self._get_pk_val(parent._meta))
  857. # Since we didn't have an instance of the parent handy set
  858. # attname directly, bypassing the descriptor. Invalidate
  859. # the related object cache, in case it's been accidentally
  860. # populated. A fresh instance will be re-built from the
  861. # database if necessary.
  862. if field.is_cached(self):
  863. field.delete_cached_value(self)
  864. return inserted
  865. def _save_table(
  866. self,
  867. raw=False,
  868. cls=None,
  869. force_insert=False,
  870. force_update=False,
  871. using=None,
  872. update_fields=None,
  873. ):
  874. """
  875. Do the heavy-lifting involved in saving. Update or insert the data
  876. for a single table.
  877. """
  878. meta = cls._meta
  879. non_pks = [f for f in meta.local_concrete_fields if not f.primary_key]
  880. if update_fields:
  881. non_pks = [
  882. f
  883. for f in non_pks
  884. if f.name in update_fields or f.attname in update_fields
  885. ]
  886. pk_val = self._get_pk_val(meta)
  887. if pk_val is None:
  888. pk_val = meta.pk.get_pk_value_on_save(self)
  889. setattr(self, meta.pk.attname, pk_val)
  890. pk_set = pk_val is not None
  891. if not pk_set and (force_update or update_fields):
  892. raise ValueError("Cannot force an update in save() with no primary key.")
  893. updated = False
  894. # Skip an UPDATE when adding an instance and primary key has a default.
  895. if (
  896. not raw
  897. and not force_insert
  898. and self._state.adding
  899. and (
  900. (meta.pk.default and meta.pk.default is not NOT_PROVIDED)
  901. or (meta.pk.db_default and meta.pk.db_default is not NOT_PROVIDED)
  902. )
  903. ):
  904. force_insert = True
  905. # If possible, try an UPDATE. If that doesn't update anything, do an INSERT.
  906. if pk_set and not force_insert:
  907. base_qs = cls._base_manager.using(using)
  908. values = [
  909. (
  910. f,
  911. None,
  912. (getattr(self, f.attname) if raw else f.pre_save(self, False)),
  913. )
  914. for f in non_pks
  915. ]
  916. forced_update = update_fields or force_update
  917. updated = self._do_update(
  918. base_qs, using, pk_val, values, update_fields, forced_update
  919. )
  920. if force_update and not updated:
  921. raise DatabaseError("Forced update did not affect any rows.")
  922. if update_fields and not updated:
  923. raise DatabaseError("Save with update_fields did not affect any rows.")
  924. if not updated:
  925. if meta.order_with_respect_to:
  926. # If this is a model with an order_with_respect_to
  927. # autopopulate the _order field
  928. field = meta.order_with_respect_to
  929. filter_args = field.get_filter_kwargs_for_object(self)
  930. self._order = (
  931. cls._base_manager.using(using)
  932. .filter(**filter_args)
  933. .aggregate(
  934. _order__max=Coalesce(
  935. ExpressionWrapper(
  936. Max("_order") + Value(1), output_field=IntegerField()
  937. ),
  938. Value(0),
  939. ),
  940. )["_order__max"]
  941. )
  942. fields = [
  943. f
  944. for f in meta.local_concrete_fields
  945. if not f.generated and (pk_set or f is not meta.auto_field)
  946. ]
  947. returning_fields = meta.db_returning_fields
  948. results = self._do_insert(
  949. cls._base_manager, using, fields, returning_fields, raw
  950. )
  951. if results:
  952. for value, field in zip(results[0], returning_fields):
  953. setattr(self, field.attname, value)
  954. return updated
  955. def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
  956. """
  957. Try to update the model. Return True if the model was updated (if an
  958. update query was done and a matching row was found in the DB).
  959. """
  960. filtered = base_qs.filter(pk=pk_val)
  961. if not values:
  962. # We can end up here when saving a model in inheritance chain where
  963. # update_fields doesn't target any field in current model. In that
  964. # case we just say the update succeeded. Another case ending up here
  965. # is a model with just PK - in that case check that the PK still
  966. # exists.
  967. return update_fields is not None or filtered.exists()
  968. if self._meta.select_on_save and not forced_update:
  969. return (
  970. filtered.exists()
  971. and
  972. # It may happen that the object is deleted from the DB right after
  973. # this check, causing the subsequent UPDATE to return zero matching
  974. # rows. The same result can occur in some rare cases when the
  975. # database returns zero despite the UPDATE being executed
  976. # successfully (a row is matched and updated). In order to
  977. # distinguish these two cases, the object's existence in the
  978. # database is again checked for if the UPDATE query returns 0.
  979. (filtered._update(values) > 0 or filtered.exists())
  980. )
  981. return filtered._update(values) > 0
  982. def _do_insert(self, manager, using, fields, returning_fields, raw):
  983. """
  984. Do an INSERT. If returning_fields is defined then this method should
  985. return the newly created data for the model.
  986. """
  987. return manager._insert(
  988. [self],
  989. fields=fields,
  990. returning_fields=returning_fields,
  991. using=using,
  992. raw=raw,
  993. )
  994. def _prepare_related_fields_for_save(self, operation_name, fields=None):
  995. # Ensure that a model instance without a PK hasn't been assigned to
  996. # a ForeignKey, GenericForeignKey or OneToOneField on this model. If
  997. # the field is nullable, allowing the save would result in silent data
  998. # loss.
  999. for field in self._meta.concrete_fields:
  1000. if fields and field not in fields:
  1001. continue
  1002. # If the related field isn't cached, then an instance hasn't been
  1003. # assigned and there's no need to worry about this check.
  1004. if field.is_relation and field.is_cached(self):
  1005. obj = getattr(self, field.name, None)
  1006. if not obj:
  1007. continue
  1008. # A pk may have been assigned manually to a model instance not
  1009. # saved to the database (or auto-generated in a case like
  1010. # UUIDField), but we allow the save to proceed and rely on the
  1011. # database to raise an IntegrityError if applicable. If
  1012. # constraints aren't supported by the database, there's the
  1013. # unavoidable risk of data corruption.
  1014. if obj.pk is None:
  1015. # Remove the object from a related instance cache.
  1016. if not field.remote_field.multiple:
  1017. field.remote_field.delete_cached_value(obj)
  1018. raise ValueError(
  1019. "%s() prohibited to prevent data loss due to unsaved "
  1020. "related object '%s'." % (operation_name, field.name)
  1021. )
  1022. elif getattr(self, field.attname) in field.empty_values:
  1023. # Set related object if it has been saved after an
  1024. # assignment.
  1025. setattr(self, field.name, obj)
  1026. # If the relationship's pk/to_field was changed, clear the
  1027. # cached relationship.
  1028. if getattr(obj, field.target_field.attname) != getattr(
  1029. self, field.attname
  1030. ):
  1031. field.delete_cached_value(self)
  1032. # GenericForeignKeys are private.
  1033. for field in self._meta.private_fields:
  1034. if fields and field not in fields:
  1035. continue
  1036. if (
  1037. field.is_relation
  1038. and field.is_cached(self)
  1039. and hasattr(field, "fk_field")
  1040. ):
  1041. obj = field.get_cached_value(self, default=None)
  1042. if obj and obj.pk is None:
  1043. raise ValueError(
  1044. f"{operation_name}() prohibited to prevent data loss due to "
  1045. f"unsaved related object '{field.name}'."
  1046. )
  1047. def delete(self, using=None, keep_parents=False):
  1048. if self.pk is None:
  1049. raise ValueError(
  1050. "%s object can't be deleted because its %s attribute is set "
  1051. "to None." % (self._meta.object_name, self._meta.pk.attname)
  1052. )
  1053. using = using or router.db_for_write(self.__class__, instance=self)
  1054. collector = Collector(using=using, origin=self)
  1055. collector.collect([self], keep_parents=keep_parents)
  1056. return collector.delete()
  1057. delete.alters_data = True
  1058. async def adelete(self, using=None, keep_parents=False):
  1059. return await sync_to_async(self.delete)(
  1060. using=using,
  1061. keep_parents=keep_parents,
  1062. )
  1063. adelete.alters_data = True
  1064. def _get_FIELD_display(self, field):
  1065. value = getattr(self, field.attname)
  1066. choices_dict = dict(make_hashable(field.flatchoices))
  1067. # force_str() to coerce lazy strings.
  1068. return force_str(
  1069. choices_dict.get(make_hashable(value), value), strings_only=True
  1070. )
  1071. def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
  1072. if not self.pk:
  1073. raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
  1074. op = "gt" if is_next else "lt"
  1075. order = "" if is_next else "-"
  1076. param = getattr(self, field.attname)
  1077. q = Q.create([(field.name, param), (f"pk__{op}", self.pk)], connector=Q.AND)
  1078. q = Q.create([q, (f"{field.name}__{op}", param)], connector=Q.OR)
  1079. qs = (
  1080. self.__class__._default_manager.using(self._state.db)
  1081. .filter(**kwargs)
  1082. .filter(q)
  1083. .order_by("%s%s" % (order, field.name), "%spk" % order)
  1084. )
  1085. try:
  1086. return qs[0]
  1087. except IndexError:
  1088. raise self.DoesNotExist(
  1089. "%s matching query does not exist." % self.__class__._meta.object_name
  1090. )
  1091. def _get_next_or_previous_in_order(self, is_next):
  1092. cachename = "__%s_order_cache" % is_next
  1093. if not hasattr(self, cachename):
  1094. op = "gt" if is_next else "lt"
  1095. order = "_order" if is_next else "-_order"
  1096. order_field = self._meta.order_with_respect_to
  1097. filter_args = order_field.get_filter_kwargs_for_object(self)
  1098. obj = (
  1099. self.__class__._default_manager.filter(**filter_args)
  1100. .filter(
  1101. **{
  1102. "_order__%s"
  1103. % op: self.__class__._default_manager.values("_order").filter(
  1104. **{self._meta.pk.name: self.pk}
  1105. )
  1106. }
  1107. )
  1108. .order_by(order)[:1]
  1109. .get()
  1110. )
  1111. setattr(self, cachename, obj)
  1112. return getattr(self, cachename)
  1113. def _get_field_value_map(self, meta, exclude=None):
  1114. if exclude is None:
  1115. exclude = set()
  1116. meta = meta or self._meta
  1117. field_map = {
  1118. field.name: Value(getattr(self, field.attname), field)
  1119. for field in meta.local_concrete_fields
  1120. if field.name not in exclude
  1121. }
  1122. if "pk" not in exclude:
  1123. field_map["pk"] = Value(self.pk, meta.pk)
  1124. return field_map
  1125. def prepare_database_save(self, field):
  1126. if self.pk is None:
  1127. raise ValueError(
  1128. "Unsaved model instance %r cannot be used in an ORM query." % self
  1129. )
  1130. return getattr(self, field.remote_field.get_related_field().attname)
  1131. def clean(self):
  1132. """
  1133. Hook for doing any extra model-wide validation after clean() has been
  1134. called on every field by self.clean_fields. Any ValidationError raised
  1135. by this method will not be associated with a particular field; it will
  1136. have a special-case association with the field defined by NON_FIELD_ERRORS.
  1137. """
  1138. pass
  1139. def validate_unique(self, exclude=None):
  1140. """
  1141. Check unique constraints on the model and raise ValidationError if any
  1142. failed.
  1143. """
  1144. unique_checks, date_checks = self._get_unique_checks(exclude=exclude)
  1145. errors = self._perform_unique_checks(unique_checks)
  1146. date_errors = self._perform_date_checks(date_checks)
  1147. for k, v in date_errors.items():
  1148. errors.setdefault(k, []).extend(v)
  1149. if errors:
  1150. raise ValidationError(errors)
  1151. def _get_unique_checks(self, exclude=None, include_meta_constraints=False):
  1152. """
  1153. Return a list of checks to perform. Since validate_unique() could be
  1154. called from a ModelForm, some fields may have been excluded; we can't
  1155. perform a unique check on a model that is missing fields involved
  1156. in that check. Fields that did not validate should also be excluded,
  1157. but they need to be passed in via the exclude argument.
  1158. """
  1159. if exclude is None:
  1160. exclude = set()
  1161. unique_checks = []
  1162. unique_togethers = [(self.__class__, self._meta.unique_together)]
  1163. constraints = []
  1164. if include_meta_constraints:
  1165. constraints = [(self.__class__, self._meta.total_unique_constraints)]
  1166. for parent_class in self._meta.get_parent_list():
  1167. if parent_class._meta.unique_together:
  1168. unique_togethers.append(
  1169. (parent_class, parent_class._meta.unique_together)
  1170. )
  1171. if include_meta_constraints and parent_class._meta.total_unique_constraints:
  1172. constraints.append(
  1173. (parent_class, parent_class._meta.total_unique_constraints)
  1174. )
  1175. for model_class, unique_together in unique_togethers:
  1176. for check in unique_together:
  1177. if not any(name in exclude for name in check):
  1178. # Add the check if the field isn't excluded.
  1179. unique_checks.append((model_class, tuple(check)))
  1180. if include_meta_constraints:
  1181. for model_class, model_constraints in constraints:
  1182. for constraint in model_constraints:
  1183. if not any(name in exclude for name in constraint.fields):
  1184. unique_checks.append((model_class, constraint.fields))
  1185. # These are checks for the unique_for_<date/year/month>.
  1186. date_checks = []
  1187. # Gather a list of checks for fields declared as unique and add them to
  1188. # the list of checks.
  1189. fields_with_class = [(self.__class__, self._meta.local_fields)]
  1190. for parent_class in self._meta.get_parent_list():
  1191. fields_with_class.append((parent_class, parent_class._meta.local_fields))
  1192. for model_class, fields in fields_with_class:
  1193. for f in fields:
  1194. name = f.name
  1195. if name in exclude:
  1196. continue
  1197. if f.unique:
  1198. unique_checks.append((model_class, (name,)))
  1199. if f.unique_for_date and f.unique_for_date not in exclude:
  1200. date_checks.append((model_class, "date", name, f.unique_for_date))
  1201. if f.unique_for_year and f.unique_for_year not in exclude:
  1202. date_checks.append((model_class, "year", name, f.unique_for_year))
  1203. if f.unique_for_month and f.unique_for_month not in exclude:
  1204. date_checks.append((model_class, "month", name, f.unique_for_month))
  1205. return unique_checks, date_checks
  1206. def _perform_unique_checks(self, unique_checks):
  1207. errors = {}
  1208. for model_class, unique_check in unique_checks:
  1209. # Try to look up an existing object with the same values as this
  1210. # object's values for all the unique field.
  1211. lookup_kwargs = {}
  1212. for field_name in unique_check:
  1213. f = self._meta.get_field(field_name)
  1214. lookup_value = getattr(self, f.attname)
  1215. # TODO: Handle multiple backends with different feature flags.
  1216. if lookup_value is None or (
  1217. lookup_value == ""
  1218. and connection.features.interprets_empty_strings_as_nulls
  1219. ):
  1220. # no value, skip the lookup
  1221. continue
  1222. if f.primary_key and not self._state.adding:
  1223. # no need to check for unique primary key when editing
  1224. continue
  1225. lookup_kwargs[str(field_name)] = lookup_value
  1226. # some fields were skipped, no reason to do the check
  1227. if len(unique_check) != len(lookup_kwargs):
  1228. continue
  1229. qs = model_class._default_manager.filter(**lookup_kwargs)
  1230. # Exclude the current object from the query if we are editing an
  1231. # instance (as opposed to creating a new one)
  1232. # Note that we need to use the pk as defined by model_class, not
  1233. # self.pk. These can be different fields because model inheritance
  1234. # allows single model to have effectively multiple primary keys.
  1235. # Refs #17615.
  1236. model_class_pk = self._get_pk_val(model_class._meta)
  1237. if not self._state.adding and model_class_pk is not None:
  1238. qs = qs.exclude(pk=model_class_pk)
  1239. if qs.exists():
  1240. if len(unique_check) == 1:
  1241. key = unique_check[0]
  1242. else:
  1243. key = NON_FIELD_ERRORS
  1244. errors.setdefault(key, []).append(
  1245. self.unique_error_message(model_class, unique_check)
  1246. )
  1247. return errors
  1248. def _perform_date_checks(self, date_checks):
  1249. errors = {}
  1250. for model_class, lookup_type, field, unique_for in date_checks:
  1251. lookup_kwargs = {}
  1252. # there's a ticket to add a date lookup, we can remove this special
  1253. # case if that makes it's way in
  1254. date = getattr(self, unique_for)
  1255. if date is None:
  1256. continue
  1257. if lookup_type == "date":
  1258. lookup_kwargs["%s__day" % unique_for] = date.day
  1259. lookup_kwargs["%s__month" % unique_for] = date.month
  1260. lookup_kwargs["%s__year" % unique_for] = date.year
  1261. else:
  1262. lookup_kwargs["%s__%s" % (unique_for, lookup_type)] = getattr(
  1263. date, lookup_type
  1264. )
  1265. lookup_kwargs[field] = getattr(self, field)
  1266. qs = model_class._default_manager.filter(**lookup_kwargs)
  1267. # Exclude the current object from the query if we are editing an
  1268. # instance (as opposed to creating a new one)
  1269. if not self._state.adding and self.pk is not None:
  1270. qs = qs.exclude(pk=self.pk)
  1271. if qs.exists():
  1272. errors.setdefault(field, []).append(
  1273. self.date_error_message(lookup_type, field, unique_for)
  1274. )
  1275. return errors
  1276. def date_error_message(self, lookup_type, field_name, unique_for):
  1277. opts = self._meta
  1278. field = opts.get_field(field_name)
  1279. return ValidationError(
  1280. message=field.error_messages["unique_for_date"],
  1281. code="unique_for_date",
  1282. params={
  1283. "model": self,
  1284. "model_name": capfirst(opts.verbose_name),
  1285. "lookup_type": lookup_type,
  1286. "field": field_name,
  1287. "field_label": capfirst(field.verbose_name),
  1288. "date_field": unique_for,
  1289. "date_field_label": capfirst(opts.get_field(unique_for).verbose_name),
  1290. },
  1291. )
  1292. def unique_error_message(self, model_class, unique_check):
  1293. opts = model_class._meta
  1294. params = {
  1295. "model": self,
  1296. "model_class": model_class,
  1297. "model_name": capfirst(opts.verbose_name),
  1298. "unique_check": unique_check,
  1299. }
  1300. # A unique field
  1301. if len(unique_check) == 1:
  1302. field = opts.get_field(unique_check[0])
  1303. params["field_label"] = capfirst(field.verbose_name)
  1304. return ValidationError(
  1305. message=field.error_messages["unique"],
  1306. code="unique",
  1307. params=params,
  1308. )
  1309. # unique_together
  1310. else:
  1311. field_labels = [
  1312. capfirst(opts.get_field(f).verbose_name) for f in unique_check
  1313. ]
  1314. params["field_labels"] = get_text_list(field_labels, _("and"))
  1315. return ValidationError(
  1316. message=_("%(model_name)s with this %(field_labels)s already exists."),
  1317. code="unique_together",
  1318. params=params,
  1319. )
  1320. def get_constraints(self):
  1321. constraints = [(self.__class__, self._meta.constraints)]
  1322. for parent_class in self._meta.get_parent_list():
  1323. if parent_class._meta.constraints:
  1324. constraints.append((parent_class, parent_class._meta.constraints))
  1325. return constraints
  1326. def validate_constraints(self, exclude=None):
  1327. constraints = self.get_constraints()
  1328. using = router.db_for_write(self.__class__, instance=self)
  1329. errors = {}
  1330. for model_class, model_constraints in constraints:
  1331. for constraint in model_constraints:
  1332. try:
  1333. constraint.validate(model_class, self, exclude=exclude, using=using)
  1334. except ValidationError as e:
  1335. if (
  1336. getattr(e, "code", None) == "unique"
  1337. and len(constraint.fields) == 1
  1338. ):
  1339. errors.setdefault(constraint.fields[0], []).append(e)
  1340. else:
  1341. errors = e.update_error_dict(errors)
  1342. if errors:
  1343. raise ValidationError(errors)
  1344. def full_clean(self, exclude=None, validate_unique=True, validate_constraints=True):
  1345. """
  1346. Call clean_fields(), clean(), validate_unique(), and
  1347. validate_constraints() on the model. Raise a ValidationError for any
  1348. errors that occur.
  1349. """
  1350. errors = {}
  1351. if exclude is None:
  1352. exclude = set()
  1353. else:
  1354. exclude = set(exclude)
  1355. try:
  1356. self.clean_fields(exclude=exclude)
  1357. except ValidationError as e:
  1358. errors = e.update_error_dict(errors)
  1359. # Form.clean() is run even if other validation fails, so do the
  1360. # same with Model.clean() for consistency.
  1361. try:
  1362. self.clean()
  1363. except ValidationError as e:
  1364. errors = e.update_error_dict(errors)
  1365. # Run unique checks, but only for fields that passed validation.
  1366. if validate_unique:
  1367. for name in errors:
  1368. if name != NON_FIELD_ERRORS and name not in exclude:
  1369. exclude.add(name)
  1370. try:
  1371. self.validate_unique(exclude=exclude)
  1372. except ValidationError as e:
  1373. errors = e.update_error_dict(errors)
  1374. # Run constraints checks, but only for fields that passed validation.
  1375. if validate_constraints:
  1376. for name in errors:
  1377. if name != NON_FIELD_ERRORS and name not in exclude:
  1378. exclude.add(name)
  1379. try:
  1380. self.validate_constraints(exclude=exclude)
  1381. except ValidationError as e:
  1382. errors = e.update_error_dict(errors)
  1383. if errors:
  1384. raise ValidationError(errors)
  1385. def clean_fields(self, exclude=None):
  1386. """
  1387. Clean all fields and raise a ValidationError containing a dict
  1388. of all validation errors if any occur.
  1389. """
  1390. if exclude is None:
  1391. exclude = set()
  1392. errors = {}
  1393. for f in self._meta.fields:
  1394. if f.name in exclude:
  1395. continue
  1396. # Skip validation for empty fields with blank=True. The developer
  1397. # is responsible for making sure they have a valid value.
  1398. raw_value = getattr(self, f.attname)
  1399. if f.blank and raw_value in f.empty_values:
  1400. continue
  1401. try:
  1402. setattr(self, f.attname, f.clean(raw_value, self))
  1403. except ValidationError as e:
  1404. errors[f.name] = e.error_list
  1405. if errors:
  1406. raise ValidationError(errors)
  1407. @classmethod
  1408. def check(cls, **kwargs):
  1409. errors = [
  1410. *cls._check_swappable(),
  1411. *cls._check_model(),
  1412. *cls._check_managers(**kwargs),
  1413. ]
  1414. if not cls._meta.swapped:
  1415. databases = kwargs.get("databases") or []
  1416. errors += [
  1417. *cls._check_fields(**kwargs),
  1418. *cls._check_m2m_through_same_relationship(),
  1419. *cls._check_long_column_names(databases),
  1420. ]
  1421. clash_errors = (
  1422. *cls._check_id_field(),
  1423. *cls._check_field_name_clashes(),
  1424. *cls._check_model_name_db_lookup_clashes(),
  1425. *cls._check_property_name_related_field_accessor_clashes(),
  1426. *cls._check_single_primary_key(),
  1427. )
  1428. errors.extend(clash_errors)
  1429. # If there are field name clashes, hide consequent column name
  1430. # clashes.
  1431. if not clash_errors:
  1432. errors.extend(cls._check_column_name_clashes())
  1433. errors += [
  1434. *cls._check_index_together(),
  1435. *cls._check_unique_together(),
  1436. *cls._check_indexes(databases),
  1437. *cls._check_ordering(),
  1438. *cls._check_constraints(databases),
  1439. *cls._check_default_pk(),
  1440. *cls._check_db_table_comment(databases),
  1441. ]
  1442. return errors
  1443. @classmethod
  1444. def _check_default_pk(cls):
  1445. if (
  1446. not cls._meta.abstract
  1447. and cls._meta.pk.auto_created
  1448. and
  1449. # Inherited PKs are checked in parents models.
  1450. not (
  1451. isinstance(cls._meta.pk, OneToOneField)
  1452. and cls._meta.pk.remote_field.parent_link
  1453. )
  1454. and not settings.is_overridden("DEFAULT_AUTO_FIELD")
  1455. and cls._meta.app_config
  1456. and not cls._meta.app_config._is_default_auto_field_overridden
  1457. ):
  1458. return [
  1459. checks.Warning(
  1460. f"Auto-created primary key used when not defining a "
  1461. f"primary key type, by default "
  1462. f"'{settings.DEFAULT_AUTO_FIELD}'.",
  1463. hint=(
  1464. f"Configure the DEFAULT_AUTO_FIELD setting or the "
  1465. f"{cls._meta.app_config.__class__.__qualname__}."
  1466. f"default_auto_field attribute to point to a subclass "
  1467. f"of AutoField, e.g. 'django.db.models.BigAutoField'."
  1468. ),
  1469. obj=cls,
  1470. id="models.W042",
  1471. ),
  1472. ]
  1473. return []
  1474. @classmethod
  1475. def _check_db_table_comment(cls, databases):
  1476. if not cls._meta.db_table_comment:
  1477. return []
  1478. errors = []
  1479. for db in databases:
  1480. if not router.allow_migrate_model(db, cls):
  1481. continue
  1482. connection = connections[db]
  1483. if not (
  1484. connection.features.supports_comments
  1485. or "supports_comments" in cls._meta.required_db_features
  1486. ):
  1487. errors.append(
  1488. checks.Warning(
  1489. f"{connection.display_name} does not support comments on "
  1490. f"tables (db_table_comment).",
  1491. obj=cls,
  1492. id="models.W046",
  1493. )
  1494. )
  1495. return errors
  1496. @classmethod
  1497. def _check_swappable(cls):
  1498. """Check if the swapped model exists."""
  1499. errors = []
  1500. if cls._meta.swapped:
  1501. try:
  1502. apps.get_model(cls._meta.swapped)
  1503. except ValueError:
  1504. errors.append(
  1505. checks.Error(
  1506. "'%s' is not of the form 'app_label.app_name'."
  1507. % cls._meta.swappable,
  1508. id="models.E001",
  1509. )
  1510. )
  1511. except LookupError:
  1512. app_label, model_name = cls._meta.swapped.split(".")
  1513. errors.append(
  1514. checks.Error(
  1515. "'%s' references '%s.%s', which has not been "
  1516. "installed, or is abstract."
  1517. % (cls._meta.swappable, app_label, model_name),
  1518. id="models.E002",
  1519. )
  1520. )
  1521. return errors
  1522. @classmethod
  1523. def _check_model(cls):
  1524. errors = []
  1525. if cls._meta.proxy:
  1526. if cls._meta.local_fields or cls._meta.local_many_to_many:
  1527. errors.append(
  1528. checks.Error(
  1529. "Proxy model '%s' contains model fields." % cls.__name__,
  1530. id="models.E017",
  1531. )
  1532. )
  1533. return errors
  1534. @classmethod
  1535. def _check_managers(cls, **kwargs):
  1536. """Perform all manager checks."""
  1537. errors = []
  1538. for manager in cls._meta.managers:
  1539. errors.extend(manager.check(**kwargs))
  1540. return errors
  1541. @classmethod
  1542. def _check_fields(cls, **kwargs):
  1543. """Perform all field checks."""
  1544. errors = []
  1545. for field in cls._meta.local_fields:
  1546. errors.extend(field.check(**kwargs))
  1547. for field in cls._meta.local_many_to_many:
  1548. errors.extend(field.check(from_model=cls, **kwargs))
  1549. return errors
  1550. @classmethod
  1551. def _check_m2m_through_same_relationship(cls):
  1552. """Check if no relationship model is used by more than one m2m field."""
  1553. errors = []
  1554. seen_intermediary_signatures = []
  1555. fields = cls._meta.local_many_to_many
  1556. # Skip when the target model wasn't found.
  1557. fields = (f for f in fields if isinstance(f.remote_field.model, ModelBase))
  1558. # Skip when the relationship model wasn't found.
  1559. fields = (f for f in fields if isinstance(f.remote_field.through, ModelBase))
  1560. for f in fields:
  1561. signature = (
  1562. f.remote_field.model,
  1563. cls,
  1564. f.remote_field.through,
  1565. f.remote_field.through_fields,
  1566. )
  1567. if signature in seen_intermediary_signatures:
  1568. errors.append(
  1569. checks.Error(
  1570. "The model has two identical many-to-many relations "
  1571. "through the intermediate model '%s'."
  1572. % f.remote_field.through._meta.label,
  1573. obj=cls,
  1574. id="models.E003",
  1575. )
  1576. )
  1577. else:
  1578. seen_intermediary_signatures.append(signature)
  1579. return errors
  1580. @classmethod
  1581. def _check_id_field(cls):
  1582. """Check if `id` field is a primary key."""
  1583. fields = [
  1584. f for f in cls._meta.local_fields if f.name == "id" and f != cls._meta.pk
  1585. ]
  1586. # fields is empty or consists of the invalid "id" field
  1587. if fields and not fields[0].primary_key and cls._meta.pk.name == "id":
  1588. return [
  1589. checks.Error(
  1590. "'id' can only be used as a field name if the field also "
  1591. "sets 'primary_key=True'.",
  1592. obj=cls,
  1593. id="models.E004",
  1594. )
  1595. ]
  1596. else:
  1597. return []
  1598. @classmethod
  1599. def _check_field_name_clashes(cls):
  1600. """Forbid field shadowing in multi-table inheritance."""
  1601. errors = []
  1602. used_fields = {} # name or attname -> field
  1603. # Check that multi-inheritance doesn't cause field name shadowing.
  1604. for parent in cls._meta.get_parent_list():
  1605. for f in parent._meta.local_fields:
  1606. clash = used_fields.get(f.name) or used_fields.get(f.attname) or None
  1607. if clash:
  1608. errors.append(
  1609. checks.Error(
  1610. "The field '%s' from parent model "
  1611. "'%s' clashes with the field '%s' "
  1612. "from parent model '%s'."
  1613. % (clash.name, clash.model._meta, f.name, f.model._meta),
  1614. obj=cls,
  1615. id="models.E005",
  1616. )
  1617. )
  1618. used_fields[f.name] = f
  1619. used_fields[f.attname] = f
  1620. # Check that fields defined in the model don't clash with fields from
  1621. # parents, including auto-generated fields like multi-table inheritance
  1622. # child accessors.
  1623. for parent in cls._meta.get_parent_list():
  1624. for f in parent._meta.get_fields():
  1625. if f not in used_fields:
  1626. used_fields[f.name] = f
  1627. # Check that parent links in diamond-shaped MTI models don't clash.
  1628. for parent_link in cls._meta.parents.values():
  1629. if not parent_link:
  1630. continue
  1631. clash = used_fields.get(parent_link.name) or None
  1632. if clash:
  1633. errors.append(
  1634. checks.Error(
  1635. f"The field '{parent_link.name}' clashes with the field "
  1636. f"'{clash.name}' from model '{clash.model._meta}'.",
  1637. obj=cls,
  1638. id="models.E006",
  1639. )
  1640. )
  1641. for f in cls._meta.local_fields:
  1642. clash = used_fields.get(f.name) or used_fields.get(f.attname) or None
  1643. # Note that we may detect clash between user-defined non-unique
  1644. # field "id" and automatically added unique field "id", both
  1645. # defined at the same model. This special case is considered in
  1646. # _check_id_field and here we ignore it.
  1647. id_conflict = (
  1648. f.name == "id" and clash and clash.name == "id" and clash.model == cls
  1649. )
  1650. if clash and not id_conflict:
  1651. errors.append(
  1652. checks.Error(
  1653. "The field '%s' clashes with the field '%s' "
  1654. "from model '%s'." % (f.name, clash.name, clash.model._meta),
  1655. obj=f,
  1656. id="models.E006",
  1657. )
  1658. )
  1659. used_fields[f.name] = f
  1660. used_fields[f.attname] = f
  1661. return errors
  1662. @classmethod
  1663. def _check_column_name_clashes(cls):
  1664. # Store a list of column names which have already been used by other fields.
  1665. used_column_names = []
  1666. errors = []
  1667. for f in cls._meta.local_fields:
  1668. _, column_name = f.get_attname_column()
  1669. # Ensure the column name is not already in use.
  1670. if column_name and column_name in used_column_names:
  1671. errors.append(
  1672. checks.Error(
  1673. "Field '%s' has column name '%s' that is used by "
  1674. "another field." % (f.name, column_name),
  1675. hint="Specify a 'db_column' for the field.",
  1676. obj=cls,
  1677. id="models.E007",
  1678. )
  1679. )
  1680. else:
  1681. used_column_names.append(column_name)
  1682. return errors
  1683. @classmethod
  1684. def _check_model_name_db_lookup_clashes(cls):
  1685. errors = []
  1686. model_name = cls.__name__
  1687. if model_name.startswith("_") or model_name.endswith("_"):
  1688. errors.append(
  1689. checks.Error(
  1690. "The model name '%s' cannot start or end with an underscore "
  1691. "as it collides with the query lookup syntax." % model_name,
  1692. obj=cls,
  1693. id="models.E023",
  1694. )
  1695. )
  1696. elif LOOKUP_SEP in model_name:
  1697. errors.append(
  1698. checks.Error(
  1699. "The model name '%s' cannot contain double underscores as "
  1700. "it collides with the query lookup syntax." % model_name,
  1701. obj=cls,
  1702. id="models.E024",
  1703. )
  1704. )
  1705. return errors
  1706. @classmethod
  1707. def _check_property_name_related_field_accessor_clashes(cls):
  1708. errors = []
  1709. property_names = cls._meta._property_names
  1710. related_field_accessors = (
  1711. f.get_attname()
  1712. for f in cls._meta._get_fields(reverse=False)
  1713. if f.is_relation and f.related_model is not None
  1714. )
  1715. for accessor in related_field_accessors:
  1716. if accessor in property_names:
  1717. errors.append(
  1718. checks.Error(
  1719. "The property '%s' clashes with a related field "
  1720. "accessor." % accessor,
  1721. obj=cls,
  1722. id="models.E025",
  1723. )
  1724. )
  1725. return errors
  1726. @classmethod
  1727. def _check_single_primary_key(cls):
  1728. errors = []
  1729. if sum(1 for f in cls._meta.local_fields if f.primary_key) > 1:
  1730. errors.append(
  1731. checks.Error(
  1732. "The model cannot have more than one field with "
  1733. "'primary_key=True'.",
  1734. obj=cls,
  1735. id="models.E026",
  1736. )
  1737. )
  1738. return errors
  1739. # RemovedInDjango51Warning.
  1740. @classmethod
  1741. def _check_index_together(cls):
  1742. """Check the value of "index_together" option."""
  1743. if not isinstance(cls._meta.index_together, (tuple, list)):
  1744. return [
  1745. checks.Error(
  1746. "'index_together' must be a list or tuple.",
  1747. obj=cls,
  1748. id="models.E008",
  1749. )
  1750. ]
  1751. elif any(
  1752. not isinstance(fields, (tuple, list)) for fields in cls._meta.index_together
  1753. ):
  1754. return [
  1755. checks.Error(
  1756. "All 'index_together' elements must be lists or tuples.",
  1757. obj=cls,
  1758. id="models.E009",
  1759. )
  1760. ]
  1761. else:
  1762. errors = []
  1763. for fields in cls._meta.index_together:
  1764. errors.extend(cls._check_local_fields(fields, "index_together"))
  1765. return errors
  1766. @classmethod
  1767. def _check_unique_together(cls):
  1768. """Check the value of "unique_together" option."""
  1769. if not isinstance(cls._meta.unique_together, (tuple, list)):
  1770. return [
  1771. checks.Error(
  1772. "'unique_together' must be a list or tuple.",
  1773. obj=cls,
  1774. id="models.E010",
  1775. )
  1776. ]
  1777. elif any(
  1778. not isinstance(fields, (tuple, list))
  1779. for fields in cls._meta.unique_together
  1780. ):
  1781. return [
  1782. checks.Error(
  1783. "All 'unique_together' elements must be lists or tuples.",
  1784. obj=cls,
  1785. id="models.E011",
  1786. )
  1787. ]
  1788. else:
  1789. errors = []
  1790. for fields in cls._meta.unique_together:
  1791. errors.extend(cls._check_local_fields(fields, "unique_together"))
  1792. return errors
  1793. @classmethod
  1794. def _check_indexes(cls, databases):
  1795. """Check fields, names, and conditions of indexes."""
  1796. errors = []
  1797. references = set()
  1798. for index in cls._meta.indexes:
  1799. # Index name can't start with an underscore or a number, restricted
  1800. # for cross-database compatibility with Oracle.
  1801. if index.name[0] == "_" or index.name[0].isdigit():
  1802. errors.append(
  1803. checks.Error(
  1804. "The index name '%s' cannot start with an underscore "
  1805. "or a number." % index.name,
  1806. obj=cls,
  1807. id="models.E033",
  1808. ),
  1809. )
  1810. if len(index.name) > index.max_name_length:
  1811. errors.append(
  1812. checks.Error(
  1813. "The index name '%s' cannot be longer than %d "
  1814. "characters." % (index.name, index.max_name_length),
  1815. obj=cls,
  1816. id="models.E034",
  1817. ),
  1818. )
  1819. if index.contains_expressions:
  1820. for expression in index.expressions:
  1821. references.update(
  1822. ref[0] for ref in cls._get_expr_references(expression)
  1823. )
  1824. for db in databases:
  1825. if not router.allow_migrate_model(db, cls):
  1826. continue
  1827. connection = connections[db]
  1828. if not (
  1829. connection.features.supports_partial_indexes
  1830. or "supports_partial_indexes" in cls._meta.required_db_features
  1831. ) and any(index.condition is not None for index in cls._meta.indexes):
  1832. errors.append(
  1833. checks.Warning(
  1834. "%s does not support indexes with conditions."
  1835. % connection.display_name,
  1836. hint=(
  1837. "Conditions will be ignored. Silence this warning "
  1838. "if you don't care about it."
  1839. ),
  1840. obj=cls,
  1841. id="models.W037",
  1842. )
  1843. )
  1844. if not (
  1845. connection.features.supports_covering_indexes
  1846. or "supports_covering_indexes" in cls._meta.required_db_features
  1847. ) and any(index.include for index in cls._meta.indexes):
  1848. errors.append(
  1849. checks.Warning(
  1850. "%s does not support indexes with non-key columns."
  1851. % connection.display_name,
  1852. hint=(
  1853. "Non-key columns will be ignored. Silence this "
  1854. "warning if you don't care about it."
  1855. ),
  1856. obj=cls,
  1857. id="models.W040",
  1858. )
  1859. )
  1860. if not (
  1861. connection.features.supports_expression_indexes
  1862. or "supports_expression_indexes" in cls._meta.required_db_features
  1863. ) and any(index.contains_expressions for index in cls._meta.indexes):
  1864. errors.append(
  1865. checks.Warning(
  1866. "%s does not support indexes on expressions."
  1867. % connection.display_name,
  1868. hint=(
  1869. "An index won't be created. Silence this warning "
  1870. "if you don't care about it."
  1871. ),
  1872. obj=cls,
  1873. id="models.W043",
  1874. )
  1875. )
  1876. fields = [
  1877. field for index in cls._meta.indexes for field, _ in index.fields_orders
  1878. ]
  1879. fields += [include for index in cls._meta.indexes for include in index.include]
  1880. fields += references
  1881. errors.extend(cls._check_local_fields(fields, "indexes"))
  1882. return errors
  1883. @classmethod
  1884. def _check_local_fields(cls, fields, option):
  1885. from django.db import models
  1886. # In order to avoid hitting the relation tree prematurely, we use our
  1887. # own fields_map instead of using get_field()
  1888. forward_fields_map = {}
  1889. for field in cls._meta._get_fields(reverse=False):
  1890. forward_fields_map[field.name] = field
  1891. if hasattr(field, "attname"):
  1892. forward_fields_map[field.attname] = field
  1893. errors = []
  1894. for field_name in fields:
  1895. try:
  1896. field = forward_fields_map[field_name]
  1897. except KeyError:
  1898. errors.append(
  1899. checks.Error(
  1900. "'%s' refers to the nonexistent field '%s'."
  1901. % (
  1902. option,
  1903. field_name,
  1904. ),
  1905. obj=cls,
  1906. id="models.E012",
  1907. )
  1908. )
  1909. else:
  1910. if isinstance(field.remote_field, models.ManyToManyRel):
  1911. errors.append(
  1912. checks.Error(
  1913. "'%s' refers to a ManyToManyField '%s', but "
  1914. "ManyToManyFields are not permitted in '%s'."
  1915. % (
  1916. option,
  1917. field_name,
  1918. option,
  1919. ),
  1920. obj=cls,
  1921. id="models.E013",
  1922. )
  1923. )
  1924. elif field not in cls._meta.local_fields:
  1925. errors.append(
  1926. checks.Error(
  1927. "'%s' refers to field '%s' which is not local to model "
  1928. "'%s'." % (option, field_name, cls._meta.object_name),
  1929. hint="This issue may be caused by multi-table inheritance.",
  1930. obj=cls,
  1931. id="models.E016",
  1932. )
  1933. )
  1934. return errors
  1935. @classmethod
  1936. def _check_ordering(cls):
  1937. """
  1938. Check "ordering" option -- is it a list of strings and do all fields
  1939. exist?
  1940. """
  1941. if cls._meta._ordering_clash:
  1942. return [
  1943. checks.Error(
  1944. "'ordering' and 'order_with_respect_to' cannot be used together.",
  1945. obj=cls,
  1946. id="models.E021",
  1947. ),
  1948. ]
  1949. if cls._meta.order_with_respect_to or not cls._meta.ordering:
  1950. return []
  1951. if not isinstance(cls._meta.ordering, (list, tuple)):
  1952. return [
  1953. checks.Error(
  1954. "'ordering' must be a tuple or list (even if you want to order by "
  1955. "only one field).",
  1956. obj=cls,
  1957. id="models.E014",
  1958. )
  1959. ]
  1960. errors = []
  1961. fields = cls._meta.ordering
  1962. # Skip expressions and '?' fields.
  1963. fields = (f for f in fields if isinstance(f, str) and f != "?")
  1964. # Convert "-field" to "field".
  1965. fields = (f.removeprefix("-") for f in fields)
  1966. # Separate related fields and non-related fields.
  1967. _fields = []
  1968. related_fields = []
  1969. for f in fields:
  1970. if LOOKUP_SEP in f:
  1971. related_fields.append(f)
  1972. else:
  1973. _fields.append(f)
  1974. fields = _fields
  1975. # Check related fields.
  1976. for field in related_fields:
  1977. _cls = cls
  1978. fld = None
  1979. for part in field.split(LOOKUP_SEP):
  1980. try:
  1981. # pk is an alias that won't be found by opts.get_field.
  1982. if part == "pk":
  1983. fld = _cls._meta.pk
  1984. else:
  1985. fld = _cls._meta.get_field(part)
  1986. if fld.is_relation:
  1987. _cls = fld.path_infos[-1].to_opts.model
  1988. else:
  1989. _cls = None
  1990. except (FieldDoesNotExist, AttributeError):
  1991. if fld is None or (
  1992. fld.get_transform(part) is None and fld.get_lookup(part) is None
  1993. ):
  1994. errors.append(
  1995. checks.Error(
  1996. "'ordering' refers to the nonexistent field, "
  1997. "related field, or lookup '%s'." % field,
  1998. obj=cls,
  1999. id="models.E015",
  2000. )
  2001. )
  2002. # Skip ordering on pk. This is always a valid order_by field
  2003. # but is an alias and therefore won't be found by opts.get_field.
  2004. fields = {f for f in fields if f != "pk"}
  2005. # Check for invalid or nonexistent fields in ordering.
  2006. invalid_fields = []
  2007. # Any field name that is not present in field_names does not exist.
  2008. # Also, ordering by m2m fields is not allowed.
  2009. opts = cls._meta
  2010. valid_fields = set(
  2011. chain.from_iterable(
  2012. (f.name, f.attname)
  2013. if not (f.auto_created and not f.concrete)
  2014. else (f.field.related_query_name(),)
  2015. for f in chain(opts.fields, opts.related_objects)
  2016. )
  2017. )
  2018. invalid_fields.extend(fields - valid_fields)
  2019. for invalid_field in invalid_fields:
  2020. errors.append(
  2021. checks.Error(
  2022. "'ordering' refers to the nonexistent field, related "
  2023. "field, or lookup '%s'." % invalid_field,
  2024. obj=cls,
  2025. id="models.E015",
  2026. )
  2027. )
  2028. return errors
  2029. @classmethod
  2030. def _check_long_column_names(cls, databases):
  2031. """
  2032. Check that any auto-generated column names are shorter than the limits
  2033. for each database in which the model will be created.
  2034. """
  2035. if not databases:
  2036. return []
  2037. errors = []
  2038. allowed_len = None
  2039. db_alias = None
  2040. # Find the minimum max allowed length among all specified db_aliases.
  2041. for db in databases:
  2042. # skip databases where the model won't be created
  2043. if not router.allow_migrate_model(db, cls):
  2044. continue
  2045. connection = connections[db]
  2046. max_name_length = connection.ops.max_name_length()
  2047. if max_name_length is None or connection.features.truncates_names:
  2048. continue
  2049. else:
  2050. if allowed_len is None:
  2051. allowed_len = max_name_length
  2052. db_alias = db
  2053. elif max_name_length < allowed_len:
  2054. allowed_len = max_name_length
  2055. db_alias = db
  2056. if allowed_len is None:
  2057. return errors
  2058. for f in cls._meta.local_fields:
  2059. _, column_name = f.get_attname_column()
  2060. # Check if auto-generated name for the field is too long
  2061. # for the database.
  2062. if (
  2063. f.db_column is None
  2064. and column_name is not None
  2065. and len(column_name) > allowed_len
  2066. ):
  2067. errors.append(
  2068. checks.Error(
  2069. 'Autogenerated column name too long for field "%s". '
  2070. 'Maximum length is "%s" for database "%s".'
  2071. % (column_name, allowed_len, db_alias),
  2072. hint="Set the column name manually using 'db_column'.",
  2073. obj=cls,
  2074. id="models.E018",
  2075. )
  2076. )
  2077. for f in cls._meta.local_many_to_many:
  2078. # Skip nonexistent models.
  2079. if isinstance(f.remote_field.through, str):
  2080. continue
  2081. # Check if auto-generated name for the M2M field is too long
  2082. # for the database.
  2083. for m2m in f.remote_field.through._meta.local_fields:
  2084. _, rel_name = m2m.get_attname_column()
  2085. if (
  2086. m2m.db_column is None
  2087. and rel_name is not None
  2088. and len(rel_name) > allowed_len
  2089. ):
  2090. errors.append(
  2091. checks.Error(
  2092. "Autogenerated column name too long for M2M field "
  2093. '"%s". Maximum length is "%s" for database "%s".'
  2094. % (rel_name, allowed_len, db_alias),
  2095. hint=(
  2096. "Use 'through' to create a separate model for "
  2097. "M2M and then set column_name using 'db_column'."
  2098. ),
  2099. obj=cls,
  2100. id="models.E019",
  2101. )
  2102. )
  2103. return errors
  2104. @classmethod
  2105. def _get_expr_references(cls, expr):
  2106. if isinstance(expr, Q):
  2107. for child in expr.children:
  2108. if isinstance(child, tuple):
  2109. lookup, value = child
  2110. yield tuple(lookup.split(LOOKUP_SEP))
  2111. yield from cls._get_expr_references(value)
  2112. else:
  2113. yield from cls._get_expr_references(child)
  2114. elif isinstance(expr, F):
  2115. yield tuple(expr.name.split(LOOKUP_SEP))
  2116. elif hasattr(expr, "get_source_expressions"):
  2117. for src_expr in expr.get_source_expressions():
  2118. yield from cls._get_expr_references(src_expr)
  2119. @classmethod
  2120. def _check_constraints(cls, databases):
  2121. errors = []
  2122. for db in databases:
  2123. if not router.allow_migrate_model(db, cls):
  2124. continue
  2125. connection = connections[db]
  2126. if not (
  2127. connection.features.supports_table_check_constraints
  2128. or "supports_table_check_constraints" in cls._meta.required_db_features
  2129. ) and any(
  2130. isinstance(constraint, CheckConstraint)
  2131. for constraint in cls._meta.constraints
  2132. ):
  2133. errors.append(
  2134. checks.Warning(
  2135. "%s does not support check constraints."
  2136. % connection.display_name,
  2137. hint=(
  2138. "A constraint won't be created. Silence this "
  2139. "warning if you don't care about it."
  2140. ),
  2141. obj=cls,
  2142. id="models.W027",
  2143. )
  2144. )
  2145. if not (
  2146. connection.features.supports_partial_indexes
  2147. or "supports_partial_indexes" in cls._meta.required_db_features
  2148. ) and any(
  2149. isinstance(constraint, UniqueConstraint)
  2150. and constraint.condition is not None
  2151. for constraint in cls._meta.constraints
  2152. ):
  2153. errors.append(
  2154. checks.Warning(
  2155. "%s does not support unique constraints with "
  2156. "conditions." % connection.display_name,
  2157. hint=(
  2158. "A constraint won't be created. Silence this "
  2159. "warning if you don't care about it."
  2160. ),
  2161. obj=cls,
  2162. id="models.W036",
  2163. )
  2164. )
  2165. if not (
  2166. connection.features.supports_deferrable_unique_constraints
  2167. or "supports_deferrable_unique_constraints"
  2168. in cls._meta.required_db_features
  2169. ) and any(
  2170. isinstance(constraint, UniqueConstraint)
  2171. and constraint.deferrable is not None
  2172. for constraint in cls._meta.constraints
  2173. ):
  2174. errors.append(
  2175. checks.Warning(
  2176. "%s does not support deferrable unique constraints."
  2177. % connection.display_name,
  2178. hint=(
  2179. "A constraint won't be created. Silence this "
  2180. "warning if you don't care about it."
  2181. ),
  2182. obj=cls,
  2183. id="models.W038",
  2184. )
  2185. )
  2186. if not (
  2187. connection.features.supports_covering_indexes
  2188. or "supports_covering_indexes" in cls._meta.required_db_features
  2189. ) and any(
  2190. isinstance(constraint, UniqueConstraint) and constraint.include
  2191. for constraint in cls._meta.constraints
  2192. ):
  2193. errors.append(
  2194. checks.Warning(
  2195. "%s does not support unique constraints with non-key "
  2196. "columns." % connection.display_name,
  2197. hint=(
  2198. "A constraint won't be created. Silence this "
  2199. "warning if you don't care about it."
  2200. ),
  2201. obj=cls,
  2202. id="models.W039",
  2203. )
  2204. )
  2205. if not (
  2206. connection.features.supports_expression_indexes
  2207. or "supports_expression_indexes" in cls._meta.required_db_features
  2208. ) and any(
  2209. isinstance(constraint, UniqueConstraint)
  2210. and constraint.contains_expressions
  2211. for constraint in cls._meta.constraints
  2212. ):
  2213. errors.append(
  2214. checks.Warning(
  2215. "%s does not support unique constraints on "
  2216. "expressions." % connection.display_name,
  2217. hint=(
  2218. "A constraint won't be created. Silence this "
  2219. "warning if you don't care about it."
  2220. ),
  2221. obj=cls,
  2222. id="models.W044",
  2223. )
  2224. )
  2225. if not (
  2226. connection.features.supports_nulls_distinct_unique_constraints
  2227. or (
  2228. "supports_nulls_distinct_unique_constraints"
  2229. in cls._meta.required_db_features
  2230. )
  2231. ) and any(
  2232. isinstance(constraint, UniqueConstraint)
  2233. and constraint.nulls_distinct is not None
  2234. for constraint in cls._meta.constraints
  2235. ):
  2236. errors.append(
  2237. checks.Warning(
  2238. "%s does not support unique constraints with "
  2239. "nulls distinct." % connection.display_name,
  2240. hint=(
  2241. "A constraint won't be created. Silence this "
  2242. "warning if you don't care about it."
  2243. ),
  2244. obj=cls,
  2245. id="models.W047",
  2246. )
  2247. )
  2248. fields = set(
  2249. chain.from_iterable(
  2250. (*constraint.fields, *constraint.include)
  2251. for constraint in cls._meta.constraints
  2252. if isinstance(constraint, UniqueConstraint)
  2253. )
  2254. )
  2255. references = set()
  2256. for constraint in cls._meta.constraints:
  2257. if isinstance(constraint, UniqueConstraint):
  2258. if (
  2259. connection.features.supports_partial_indexes
  2260. or "supports_partial_indexes"
  2261. not in cls._meta.required_db_features
  2262. ) and isinstance(constraint.condition, Q):
  2263. references.update(
  2264. cls._get_expr_references(constraint.condition)
  2265. )
  2266. if (
  2267. connection.features.supports_expression_indexes
  2268. or "supports_expression_indexes"
  2269. not in cls._meta.required_db_features
  2270. ) and constraint.contains_expressions:
  2271. for expression in constraint.expressions:
  2272. references.update(cls._get_expr_references(expression))
  2273. elif isinstance(constraint, CheckConstraint):
  2274. if (
  2275. connection.features.supports_table_check_constraints
  2276. or "supports_table_check_constraints"
  2277. not in cls._meta.required_db_features
  2278. ):
  2279. if isinstance(constraint.check, Q):
  2280. references.update(
  2281. cls._get_expr_references(constraint.check)
  2282. )
  2283. if any(
  2284. isinstance(expr, RawSQL)
  2285. for expr in constraint.check.flatten()
  2286. ):
  2287. errors.append(
  2288. checks.Warning(
  2289. f"Check constraint {constraint.name!r} contains "
  2290. f"RawSQL() expression and won't be validated "
  2291. f"during the model full_clean().",
  2292. hint=(
  2293. "Silence this warning if you don't care about "
  2294. "it."
  2295. ),
  2296. obj=cls,
  2297. id="models.W045",
  2298. ),
  2299. )
  2300. for field_name, *lookups in references:
  2301. # pk is an alias that won't be found by opts.get_field.
  2302. if field_name != "pk":
  2303. fields.add(field_name)
  2304. if not lookups:
  2305. # If it has no lookups it cannot result in a JOIN.
  2306. continue
  2307. try:
  2308. if field_name == "pk":
  2309. field = cls._meta.pk
  2310. else:
  2311. field = cls._meta.get_field(field_name)
  2312. if not field.is_relation or field.many_to_many or field.one_to_many:
  2313. continue
  2314. except FieldDoesNotExist:
  2315. continue
  2316. # JOIN must happen at the first lookup.
  2317. first_lookup = lookups[0]
  2318. if (
  2319. hasattr(field, "get_transform")
  2320. and hasattr(field, "get_lookup")
  2321. and field.get_transform(first_lookup) is None
  2322. and field.get_lookup(first_lookup) is None
  2323. ):
  2324. errors.append(
  2325. checks.Error(
  2326. "'constraints' refers to the joined field '%s'."
  2327. % LOOKUP_SEP.join([field_name] + lookups),
  2328. obj=cls,
  2329. id="models.E041",
  2330. )
  2331. )
  2332. errors.extend(cls._check_local_fields(fields, "constraints"))
  2333. return errors
  2334. ############################################
  2335. # HELPER FUNCTIONS (CURRIED MODEL METHODS) #
  2336. ############################################
  2337. # ORDERING METHODS #########################
  2338. def method_set_order(self, ordered_obj, id_list, using=None):
  2339. order_wrt = ordered_obj._meta.order_with_respect_to
  2340. filter_args = order_wrt.get_forward_related_filter(self)
  2341. ordered_obj.objects.db_manager(using).filter(**filter_args).bulk_update(
  2342. [ordered_obj(pk=pk, _order=order) for order, pk in enumerate(id_list)],
  2343. ["_order"],
  2344. )
  2345. def method_get_order(self, ordered_obj):
  2346. order_wrt = ordered_obj._meta.order_with_respect_to
  2347. filter_args = order_wrt.get_forward_related_filter(self)
  2348. pk_name = ordered_obj._meta.pk.name
  2349. return ordered_obj.objects.filter(**filter_args).values_list(pk_name, flat=True)
  2350. def make_foreign_order_accessors(model, related_model):
  2351. setattr(
  2352. related_model,
  2353. "get_%s_order" % model.__name__.lower(),
  2354. partialmethod(method_get_order, model),
  2355. )
  2356. setattr(
  2357. related_model,
  2358. "set_%s_order" % model.__name__.lower(),
  2359. partialmethod(method_set_order, model),
  2360. )
  2361. ########
  2362. # MISC #
  2363. ########
  2364. def model_unpickle(model_id):
  2365. """Used to unpickle Model subclasses with deferred fields."""
  2366. if isinstance(model_id, tuple):
  2367. model = apps.get_model(*model_id)
  2368. else:
  2369. # Backwards compat - the model was cached directly in earlier versions.
  2370. model = model_id
  2371. return model.__new__(model)
  2372. model_unpickle.__safe_for_unpickle__ = True