Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 70KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. import copy
  2. import inspect
  3. import warnings
  4. from functools import partialmethod
  5. from itertools import chain
  6. from django.apps import apps
  7. from django.conf import settings
  8. from django.core import checks
  9. from django.core.exceptions import (
  10. NON_FIELD_ERRORS, FieldDoesNotExist, FieldError, MultipleObjectsReturned,
  11. ObjectDoesNotExist, ValidationError,
  12. )
  13. from django.db import (
  14. DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY, DatabaseError, connection,
  15. connections, router, transaction,
  16. )
  17. from django.db.models.constants import LOOKUP_SEP
  18. from django.db.models.deletion import CASCADE, Collector
  19. from django.db.models.fields.related import (
  20. ForeignObjectRel, OneToOneField, lazy_related_operation, resolve_relation,
  21. )
  22. from django.db.models.manager import Manager
  23. from django.db.models.options import Options
  24. from django.db.models.query import Q
  25. from django.db.models.signals import (
  26. class_prepared, post_init, post_save, pre_init, pre_save,
  27. )
  28. from django.db.models.utils import make_model_tuple
  29. from django.utils.encoding import force_text
  30. from django.utils.text import capfirst, get_text_list
  31. from django.utils.translation import gettext_lazy as _
  32. from django.utils.version import get_version
  33. class Deferred:
  34. def __repr__(self):
  35. return '<Deferred field>'
  36. def __str__(self):
  37. return '<Deferred field>'
  38. DEFERRED = Deferred()
  39. def subclass_exception(name, bases, module, attached_to):
  40. """
  41. Create exception subclass. Used by ModelBase below.
  42. The exception is created in a way that allows it to be pickled, assuming
  43. that the returned exception class will be added as an attribute to the
  44. 'attached_to' class.
  45. """
  46. return type(name, bases, {
  47. '__module__': module,
  48. '__qualname__': '%s.%s' % (attached_to.__qualname__, name),
  49. })
  50. class ModelBase(type):
  51. """Metaclass for all models."""
  52. def __new__(cls, name, bases, attrs, **kwargs):
  53. super_new = super().__new__
  54. # Also ensure initialization is only performed for subclasses of Model
  55. # (excluding Model class itself).
  56. parents = [b for b in bases if isinstance(b, ModelBase)]
  57. if not parents:
  58. return super_new(cls, name, bases, attrs)
  59. # Create the class.
  60. module = attrs.pop('__module__')
  61. new_attrs = {'__module__': module}
  62. classcell = attrs.pop('__classcell__', None)
  63. if classcell is not None:
  64. new_attrs['__classcell__'] = classcell
  65. new_class = super_new(cls, name, bases, new_attrs, **kwargs)
  66. attr_meta = attrs.pop('Meta', None)
  67. abstract = getattr(attr_meta, 'abstract', False)
  68. meta = attr_meta or getattr(new_class, 'Meta', None)
  69. base_meta = getattr(new_class, '_meta', None)
  70. app_label = None
  71. # Look for an application configuration to attach the model to.
  72. app_config = apps.get_containing_app_config(module)
  73. if getattr(meta, 'app_label', None) is None:
  74. if app_config is None:
  75. if not abstract:
  76. raise RuntimeError(
  77. "Model class %s.%s doesn't declare an explicit "
  78. "app_label and isn't in an application in "
  79. "INSTALLED_APPS." % (module, name)
  80. )
  81. else:
  82. app_label = app_config.label
  83. new_class.add_to_class('_meta', Options(meta, app_label))
  84. if not abstract:
  85. new_class.add_to_class(
  86. 'DoesNotExist',
  87. subclass_exception(
  88. 'DoesNotExist',
  89. tuple(
  90. x.DoesNotExist for x in parents if hasattr(x, '_meta') and not x._meta.abstract
  91. ) or (ObjectDoesNotExist,),
  92. module,
  93. attached_to=new_class))
  94. new_class.add_to_class(
  95. 'MultipleObjectsReturned',
  96. subclass_exception(
  97. 'MultipleObjectsReturned',
  98. tuple(
  99. x.MultipleObjectsReturned for x in parents if hasattr(x, '_meta') and not x._meta.abstract
  100. ) or (MultipleObjectsReturned,),
  101. module,
  102. attached_to=new_class))
  103. if base_meta and not base_meta.abstract:
  104. # Non-abstract child classes inherit some attributes from their
  105. # non-abstract parent (unless an ABC comes before it in the
  106. # method resolution order).
  107. if not hasattr(meta, 'ordering'):
  108. new_class._meta.ordering = base_meta.ordering
  109. if not hasattr(meta, 'get_latest_by'):
  110. new_class._meta.get_latest_by = base_meta.get_latest_by
  111. is_proxy = new_class._meta.proxy
  112. # If the model is a proxy, ensure that the base class
  113. # hasn't been swapped out.
  114. if is_proxy and base_meta and base_meta.swapped:
  115. raise TypeError("%s cannot proxy the swapped model '%s'." % (name, base_meta.swapped))
  116. # Add all attributes to the class.
  117. for obj_name, obj in attrs.items():
  118. new_class.add_to_class(obj_name, obj)
  119. # All the fields of any type declared on this model
  120. new_fields = chain(
  121. new_class._meta.local_fields,
  122. new_class._meta.local_many_to_many,
  123. new_class._meta.private_fields
  124. )
  125. field_names = {f.name for f in new_fields}
  126. # Basic setup for proxy models.
  127. if is_proxy:
  128. base = None
  129. for parent in [kls for kls in parents if hasattr(kls, '_meta')]:
  130. if parent._meta.abstract:
  131. if parent._meta.fields:
  132. raise TypeError(
  133. "Abstract base class containing model fields not "
  134. "permitted for proxy model '%s'." % name
  135. )
  136. else:
  137. continue
  138. if base is None:
  139. base = parent
  140. elif parent._meta.concrete_model is not base._meta.concrete_model:
  141. raise TypeError("Proxy model '%s' has more than one non-abstract model base class." % name)
  142. if base is None:
  143. raise TypeError("Proxy model '%s' has no non-abstract model base class." % name)
  144. new_class._meta.setup_proxy(base)
  145. new_class._meta.concrete_model = base._meta.concrete_model
  146. else:
  147. new_class._meta.concrete_model = new_class
  148. # Collect the parent links for multi-table inheritance.
  149. parent_links = {}
  150. for base in reversed([new_class] + parents):
  151. # Conceptually equivalent to `if base is Model`.
  152. if not hasattr(base, '_meta'):
  153. continue
  154. # Skip concrete parent classes.
  155. if base != new_class and not base._meta.abstract:
  156. continue
  157. # Locate OneToOneField instances.
  158. for field in base._meta.local_fields:
  159. if isinstance(field, OneToOneField):
  160. related = resolve_relation(new_class, field.remote_field.model)
  161. parent_links[make_model_tuple(related)] = field
  162. # Track fields inherited from base models.
  163. inherited_attributes = set()
  164. # Do the appropriate setup for any model parents.
  165. for base in new_class.mro():
  166. if base not in parents or not hasattr(base, '_meta'):
  167. # Things without _meta aren't functional models, so they're
  168. # uninteresting parents.
  169. inherited_attributes.update(base.__dict__)
  170. continue
  171. parent_fields = base._meta.local_fields + base._meta.local_many_to_many
  172. if not base._meta.abstract:
  173. # Check for clashes between locally declared fields and those
  174. # on the base classes.
  175. for field in parent_fields:
  176. if field.name in field_names:
  177. raise FieldError(
  178. 'Local field %r in class %r clashes with field of '
  179. 'the same name from base class %r.' % (
  180. field.name,
  181. name,
  182. base.__name__,
  183. )
  184. )
  185. else:
  186. inherited_attributes.add(field.name)
  187. # Concrete classes...
  188. base = base._meta.concrete_model
  189. base_key = make_model_tuple(base)
  190. if base_key in parent_links:
  191. field = parent_links[base_key]
  192. elif not is_proxy:
  193. attr_name = '%s_ptr' % base._meta.model_name
  194. field = OneToOneField(
  195. base,
  196. on_delete=CASCADE,
  197. name=attr_name,
  198. auto_created=True,
  199. parent_link=True,
  200. )
  201. if attr_name in field_names:
  202. raise FieldError(
  203. "Auto-generated field '%s' in class %r for "
  204. "parent_link to base class %r clashes with "
  205. "declared field of the same name." % (
  206. attr_name,
  207. name,
  208. base.__name__,
  209. )
  210. )
  211. # Only add the ptr field if it's not already present;
  212. # e.g. migrations will already have it specified
  213. if not hasattr(new_class, attr_name):
  214. new_class.add_to_class(attr_name, field)
  215. else:
  216. field = None
  217. new_class._meta.parents[base] = field
  218. else:
  219. base_parents = base._meta.parents.copy()
  220. # Add fields from abstract base class if it wasn't overridden.
  221. for field in parent_fields:
  222. if (field.name not in field_names and
  223. field.name not in new_class.__dict__ and
  224. field.name not in inherited_attributes):
  225. new_field = copy.deepcopy(field)
  226. new_class.add_to_class(field.name, new_field)
  227. # Replace parent links defined on this base by the new
  228. # field. It will be appropriately resolved if required.
  229. if field.one_to_one:
  230. for parent, parent_link in base_parents.items():
  231. if field == parent_link:
  232. base_parents[parent] = new_field
  233. # Pass any non-abstract parent classes onto child.
  234. new_class._meta.parents.update(base_parents)
  235. # Inherit private fields (like GenericForeignKey) from the parent
  236. # class
  237. for field in base._meta.private_fields:
  238. if field.name in field_names:
  239. if not base._meta.abstract:
  240. raise FieldError(
  241. 'Local field %r in class %r clashes with field of '
  242. 'the same name from base class %r.' % (
  243. field.name,
  244. name,
  245. base.__name__,
  246. )
  247. )
  248. else:
  249. field = copy.deepcopy(field)
  250. if not base._meta.abstract:
  251. field.mti_inherited = True
  252. new_class.add_to_class(field.name, field)
  253. # Copy indexes so that index names are unique when models extend an
  254. # abstract model.
  255. new_class._meta.indexes = [copy.deepcopy(idx) for idx in new_class._meta.indexes]
  256. if abstract:
  257. # Abstract base models can't be instantiated and don't appear in
  258. # the list of models for an app. We do the final setup for them a
  259. # little differently from normal models.
  260. attr_meta.abstract = False
  261. new_class.Meta = attr_meta
  262. return new_class
  263. new_class._prepare()
  264. new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  265. return new_class
  266. def add_to_class(cls, name, value):
  267. # We should call the contribute_to_class method only if it's bound
  268. if not inspect.isclass(value) and hasattr(value, 'contribute_to_class'):
  269. value.contribute_to_class(cls, name)
  270. else:
  271. setattr(cls, name, value)
  272. def _prepare(cls):
  273. """Create some methods once self._meta has been populated."""
  274. opts = cls._meta
  275. opts._prepare(cls)
  276. if opts.order_with_respect_to:
  277. cls.get_next_in_order = partialmethod(cls._get_next_or_previous_in_order, is_next=True)
  278. cls.get_previous_in_order = partialmethod(cls._get_next_or_previous_in_order, is_next=False)
  279. # Defer creating accessors on the foreign class until it has been
  280. # created and registered. If remote_field is None, we're ordering
  281. # with respect to a GenericForeignKey and don't know what the
  282. # foreign class is - we'll add those accessors later in
  283. # contribute_to_class().
  284. if opts.order_with_respect_to.remote_field:
  285. wrt = opts.order_with_respect_to
  286. remote = wrt.remote_field.model
  287. lazy_related_operation(make_foreign_order_accessors, cls, remote)
  288. # Give the class a docstring -- its definition.
  289. if cls.__doc__ is None:
  290. cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join(f.name for f in opts.fields))
  291. get_absolute_url_override = settings.ABSOLUTE_URL_OVERRIDES.get(opts.label_lower)
  292. if get_absolute_url_override:
  293. setattr(cls, 'get_absolute_url', get_absolute_url_override)
  294. if not opts.managers:
  295. if any(f.name == 'objects' for f in opts.fields):
  296. raise ValueError(
  297. "Model %s must specify a custom Manager, because it has a "
  298. "field named 'objects'." % cls.__name__
  299. )
  300. manager = Manager()
  301. manager.auto_created = True
  302. cls.add_to_class('objects', manager)
  303. # Set the name of _meta.indexes. This can't be done in
  304. # Options.contribute_to_class() because fields haven't been added to
  305. # the model at that point.
  306. for index in cls._meta.indexes:
  307. if not index.name:
  308. index.set_name_with_model(cls)
  309. class_prepared.send(sender=cls)
  310. @property
  311. def _base_manager(cls):
  312. return cls._meta.base_manager
  313. @property
  314. def _default_manager(cls):
  315. return cls._meta.default_manager
  316. class ModelStateFieldsCacheDescriptor:
  317. def __get__(self, instance, cls=None):
  318. if instance is None:
  319. return self
  320. res = instance.fields_cache = {}
  321. return res
  322. class ModelState:
  323. """Store model instance state."""
  324. db = None
  325. # If true, uniqueness validation checks will consider this a new, unsaved
  326. # object. Necessary for correct validation of new instances of objects with
  327. # explicit (non-auto) PKs. This impacts validation only; it has no effect
  328. # on the actual save.
  329. adding = True
  330. fields_cache = ModelStateFieldsCacheDescriptor()
  331. class Model(metaclass=ModelBase):
  332. def __init__(self, *args, **kwargs):
  333. # Alias some things as locals to avoid repeat global lookups
  334. cls = self.__class__
  335. opts = self._meta
  336. _setattr = setattr
  337. _DEFERRED = DEFERRED
  338. pre_init.send(sender=cls, args=args, kwargs=kwargs)
  339. # Set up the storage for instance state
  340. self._state = ModelState()
  341. # There is a rather weird disparity here; if kwargs, it's set, then args
  342. # overrides it. It should be one or the other; don't duplicate the work
  343. # The reason for the kwargs check is that standard iterator passes in by
  344. # args, and instantiation for iteration is 33% faster.
  345. if len(args) > len(opts.concrete_fields):
  346. # Daft, but matches old exception sans the err msg.
  347. raise IndexError("Number of args exceeds number of fields")
  348. if not kwargs:
  349. fields_iter = iter(opts.concrete_fields)
  350. # The ordering of the zip calls matter - zip throws StopIteration
  351. # when an iter throws it. So if the first iter throws it, the second
  352. # is *not* consumed. We rely on this, so don't change the order
  353. # without changing the logic.
  354. for val, field in zip(args, fields_iter):
  355. if val is _DEFERRED:
  356. continue
  357. _setattr(self, field.attname, val)
  358. else:
  359. # Slower, kwargs-ready version.
  360. fields_iter = iter(opts.fields)
  361. for val, field in zip(args, fields_iter):
  362. if val is _DEFERRED:
  363. continue
  364. _setattr(self, field.attname, val)
  365. kwargs.pop(field.name, None)
  366. # Now we're left with the unprocessed fields that *must* come from
  367. # keywords, or default.
  368. for field in fields_iter:
  369. is_related_object = False
  370. # Virtual field
  371. if field.attname not in kwargs and field.column is None:
  372. continue
  373. if kwargs:
  374. if isinstance(field.remote_field, ForeignObjectRel):
  375. try:
  376. # Assume object instance was passed in.
  377. rel_obj = kwargs.pop(field.name)
  378. is_related_object = True
  379. except KeyError:
  380. try:
  381. # Object instance wasn't passed in -- must be an ID.
  382. val = kwargs.pop(field.attname)
  383. except KeyError:
  384. val = field.get_default()
  385. else:
  386. # Object instance was passed in. Special case: You can
  387. # pass in "None" for related objects if it's allowed.
  388. if rel_obj is None and field.null:
  389. val = None
  390. else:
  391. try:
  392. val = kwargs.pop(field.attname)
  393. except KeyError:
  394. # This is done with an exception rather than the
  395. # default argument on pop because we don't want
  396. # get_default() to be evaluated, and then not used.
  397. # Refs #12057.
  398. val = field.get_default()
  399. else:
  400. val = field.get_default()
  401. if is_related_object:
  402. # If we are passed a related instance, set it using the
  403. # field.name instead of field.attname (e.g. "user" instead of
  404. # "user_id") so that the object gets properly cached (and type
  405. # checked) by the RelatedObjectDescriptor.
  406. if rel_obj is not _DEFERRED:
  407. _setattr(self, field.name, rel_obj)
  408. else:
  409. if val is not _DEFERRED:
  410. _setattr(self, field.attname, val)
  411. if kwargs:
  412. property_names = opts._property_names
  413. for prop in tuple(kwargs):
  414. try:
  415. # Any remaining kwargs must correspond to properties or
  416. # virtual fields.
  417. if prop in property_names or opts.get_field(prop):
  418. if kwargs[prop] is not _DEFERRED:
  419. _setattr(self, prop, kwargs[prop])
  420. del kwargs[prop]
  421. except (AttributeError, FieldDoesNotExist):
  422. pass
  423. for kwarg in kwargs:
  424. raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg)
  425. super().__init__()
  426. post_init.send(sender=cls, instance=self)
  427. @classmethod
  428. def from_db(cls, db, field_names, values):
  429. if len(values) != len(cls._meta.concrete_fields):
  430. values_iter = iter(values)
  431. values = [
  432. next(values_iter) if f.attname in field_names else DEFERRED
  433. for f in cls._meta.concrete_fields
  434. ]
  435. new = cls(*values)
  436. new._state.adding = False
  437. new._state.db = db
  438. return new
  439. def __repr__(self):
  440. return '<%s: %s>' % (self.__class__.__name__, self)
  441. def __str__(self):
  442. return '%s object (%s)' % (self.__class__.__name__, self.pk)
  443. def __eq__(self, other):
  444. if not isinstance(other, Model):
  445. return False
  446. if self._meta.concrete_model != other._meta.concrete_model:
  447. return False
  448. my_pk = self.pk
  449. if my_pk is None:
  450. return self is other
  451. return my_pk == other.pk
  452. def __hash__(self):
  453. if self.pk is None:
  454. raise TypeError("Model instances without primary key value are unhashable")
  455. return hash(self.pk)
  456. def __reduce__(self):
  457. data = self.__getstate__()
  458. data[DJANGO_VERSION_PICKLE_KEY] = get_version()
  459. class_id = self._meta.app_label, self._meta.object_name
  460. return model_unpickle, (class_id,), data
  461. def __getstate__(self):
  462. """Hook to allow choosing the attributes to pickle."""
  463. return self.__dict__
  464. def __setstate__(self, state):
  465. msg = None
  466. pickled_version = state.get(DJANGO_VERSION_PICKLE_KEY)
  467. if pickled_version:
  468. current_version = get_version()
  469. if current_version != pickled_version:
  470. msg = (
  471. "Pickled model instance's Django version %s does not match "
  472. "the current version %s." % (pickled_version, current_version)
  473. )
  474. else:
  475. msg = "Pickled model instance's Django version is not specified."
  476. if msg:
  477. warnings.warn(msg, RuntimeWarning, stacklevel=2)
  478. self.__dict__.update(state)
  479. def _get_pk_val(self, meta=None):
  480. meta = meta or self._meta
  481. return getattr(self, meta.pk.attname)
  482. def _set_pk_val(self, value):
  483. return setattr(self, self._meta.pk.attname, value)
  484. pk = property(_get_pk_val, _set_pk_val)
  485. def get_deferred_fields(self):
  486. """
  487. Return a set containing names of deferred fields for this instance.
  488. """
  489. return {
  490. f.attname for f in self._meta.concrete_fields
  491. if f.attname not in self.__dict__
  492. }
  493. def refresh_from_db(self, using=None, fields=None):
  494. """
  495. Reload field values from the database.
  496. By default, the reloading happens from the database this instance was
  497. loaded from, or by the read router if this instance wasn't loaded from
  498. any database. The using parameter will override the default.
  499. Fields can be used to specify which fields to reload. The fields
  500. should be an iterable of field attnames. If fields is None, then
  501. all non-deferred fields are reloaded.
  502. When accessing deferred fields of an instance, the deferred loading
  503. of the field will call this method.
  504. """
  505. if fields is not None:
  506. if not fields:
  507. return
  508. if any(LOOKUP_SEP in f for f in fields):
  509. raise ValueError(
  510. 'Found "%s" in fields argument. Relations and transforms '
  511. 'are not allowed in fields.' % LOOKUP_SEP)
  512. hints = {'instance': self}
  513. db_instance_qs = self.__class__._base_manager.db_manager(using, hints=hints).filter(pk=self.pk)
  514. # Use provided fields, if not set then reload all non-deferred fields.
  515. deferred_fields = self.get_deferred_fields()
  516. if fields is not None:
  517. fields = list(fields)
  518. db_instance_qs = db_instance_qs.only(*fields)
  519. elif deferred_fields:
  520. fields = [f.attname for f in self._meta.concrete_fields
  521. if f.attname not in deferred_fields]
  522. db_instance_qs = db_instance_qs.only(*fields)
  523. db_instance = db_instance_qs.get()
  524. non_loaded_fields = db_instance.get_deferred_fields()
  525. for field in self._meta.concrete_fields:
  526. if field.attname in non_loaded_fields:
  527. # This field wasn't refreshed - skip ahead.
  528. continue
  529. setattr(self, field.attname, getattr(db_instance, field.attname))
  530. # Clear cached foreign keys.
  531. if field.is_relation and field.is_cached(self):
  532. field.delete_cached_value(self)
  533. # Clear cached relations.
  534. for field in self._meta.related_objects:
  535. if field.is_cached(self):
  536. field.delete_cached_value(self)
  537. self._state.db = db_instance._state.db
  538. def serializable_value(self, field_name):
  539. """
  540. Return the value of the field name for this instance. If the field is
  541. a foreign key, return the id value instead of the object. If there's
  542. no Field object with this name on the model, return the model
  543. attribute's value.
  544. Used to serialize a field's value (in the serializer, or form output,
  545. for example). Normally, you would just access the attribute directly
  546. and not use this method.
  547. """
  548. try:
  549. field = self._meta.get_field(field_name)
  550. except FieldDoesNotExist:
  551. return getattr(self, field_name)
  552. return getattr(self, field.attname)
  553. def save(self, force_insert=False, force_update=False, using=None,
  554. update_fields=None):
  555. """
  556. Save the current instance. Override this in a subclass if you want to
  557. control the saving process.
  558. The 'force_insert' and 'force_update' parameters can be used to insist
  559. that the "save" must be an SQL insert or update (or equivalent for
  560. non-SQL backends), respectively. Normally, they should not be set.
  561. """
  562. # Ensure that a model instance without a PK hasn't been assigned to
  563. # a ForeignKey or OneToOneField on this model. If the field is
  564. # nullable, allowing the save() would result in silent data loss.
  565. for field in self._meta.concrete_fields:
  566. # If the related field isn't cached, then an instance hasn't
  567. # been assigned and there's no need to worry about this check.
  568. if field.is_relation and field.is_cached(self):
  569. obj = getattr(self, field.name, None)
  570. # A pk may have been assigned manually to a model instance not
  571. # saved to the database (or auto-generated in a case like
  572. # UUIDField), but we allow the save to proceed and rely on the
  573. # database to raise an IntegrityError if applicable. If
  574. # constraints aren't supported by the database, there's the
  575. # unavoidable risk of data corruption.
  576. if obj and obj.pk is None:
  577. # Remove the object from a related instance cache.
  578. if not field.remote_field.multiple:
  579. field.remote_field.delete_cached_value(obj)
  580. raise ValueError(
  581. "save() prohibited to prevent data loss due to "
  582. "unsaved related object '%s'." % field.name
  583. )
  584. # If the relationship's pk was changed, clear the cached
  585. # relationship.
  586. if obj and obj.pk != getattr(self, field.attname):
  587. field.delete_cached_value(self)
  588. using = using or router.db_for_write(self.__class__, instance=self)
  589. if force_insert and (force_update or update_fields):
  590. raise ValueError("Cannot force both insert and updating in model saving.")
  591. deferred_fields = self.get_deferred_fields()
  592. if update_fields is not None:
  593. # If update_fields is empty, skip the save. We do also check for
  594. # no-op saves later on for inheritance cases. This bailout is
  595. # still needed for skipping signal sending.
  596. if not update_fields:
  597. return
  598. update_fields = frozenset(update_fields)
  599. field_names = set()
  600. for field in self._meta.fields:
  601. if not field.primary_key:
  602. field_names.add(field.name)
  603. if field.name != field.attname:
  604. field_names.add(field.attname)
  605. non_model_fields = update_fields.difference(field_names)
  606. if non_model_fields:
  607. raise ValueError("The following fields do not exist in this "
  608. "model or are m2m fields: %s"
  609. % ', '.join(non_model_fields))
  610. # If saving to the same database, and this model is deferred, then
  611. # automatically do a "update_fields" save on the loaded fields.
  612. elif not force_insert and deferred_fields and using == self._state.db:
  613. field_names = set()
  614. for field in self._meta.concrete_fields:
  615. if not field.primary_key and not hasattr(field, 'through'):
  616. field_names.add(field.attname)
  617. loaded_fields = field_names.difference(deferred_fields)
  618. if loaded_fields:
  619. update_fields = frozenset(loaded_fields)
  620. self.save_base(using=using, force_insert=force_insert,
  621. force_update=force_update, update_fields=update_fields)
  622. save.alters_data = True
  623. def save_base(self, raw=False, force_insert=False,
  624. force_update=False, using=None, update_fields=None):
  625. """
  626. Handle the parts of saving which should be done only once per save,
  627. yet need to be done in raw saves, too. This includes some sanity
  628. checks and signal sending.
  629. The 'raw' argument is telling save_base not to save any parent
  630. models and not to do any changes to the values before save. This
  631. is used by fixture loading.
  632. """
  633. using = using or router.db_for_write(self.__class__, instance=self)
  634. assert not (force_insert and (force_update or update_fields))
  635. assert update_fields is None or update_fields
  636. cls = origin = self.__class__
  637. # Skip proxies, but keep the origin as the proxy model.
  638. if cls._meta.proxy:
  639. cls = cls._meta.concrete_model
  640. meta = cls._meta
  641. if not meta.auto_created:
  642. pre_save.send(
  643. sender=origin, instance=self, raw=raw, using=using,
  644. update_fields=update_fields,
  645. )
  646. with transaction.atomic(using=using, savepoint=False):
  647. if not raw:
  648. self._save_parents(cls, using, update_fields)
  649. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  650. # Store the database on which the object was saved
  651. self._state.db = using
  652. # Once saved, this is no longer a to-be-added instance.
  653. self._state.adding = False
  654. # Signal that the save is complete
  655. if not meta.auto_created:
  656. post_save.send(
  657. sender=origin, instance=self, created=(not updated),
  658. update_fields=update_fields, raw=raw, using=using,
  659. )
  660. save_base.alters_data = True
  661. def _save_parents(self, cls, using, update_fields):
  662. """Save all the parents of cls using values from self."""
  663. meta = cls._meta
  664. for parent, field in meta.parents.items():
  665. # Make sure the link fields are synced between parent and self.
  666. if (field and getattr(self, parent._meta.pk.attname) is None and
  667. getattr(self, field.attname) is not None):
  668. setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
  669. self._save_parents(cls=parent, using=using, update_fields=update_fields)
  670. self._save_table(cls=parent, using=using, update_fields=update_fields)
  671. # Set the parent's PK value to self.
  672. if field:
  673. setattr(self, field.attname, self._get_pk_val(parent._meta))
  674. # Since we didn't have an instance of the parent handy set
  675. # attname directly, bypassing the descriptor. Invalidate
  676. # the related object cache, in case it's been accidentally
  677. # populated. A fresh instance will be re-built from the
  678. # database if necessary.
  679. if field.is_cached(self):
  680. field.delete_cached_value(self)
  681. def _save_table(self, raw=False, cls=None, force_insert=False,
  682. force_update=False, using=None, update_fields=None):
  683. """
  684. Do the heavy-lifting involved in saving. Update or insert the data
  685. for a single table.
  686. """
  687. meta = cls._meta
  688. non_pks = [f for f in meta.local_concrete_fields if not f.primary_key]
  689. if update_fields:
  690. non_pks = [f for f in non_pks
  691. if f.name in update_fields or f.attname in update_fields]
  692. pk_val = self._get_pk_val(meta)
  693. if pk_val is None:
  694. pk_val = meta.pk.get_pk_value_on_save(self)
  695. setattr(self, meta.pk.attname, pk_val)
  696. pk_set = pk_val is not None
  697. if not pk_set and (force_update or update_fields):
  698. raise ValueError("Cannot force an update in save() with no primary key.")
  699. updated = False
  700. # If possible, try an UPDATE. If that doesn't update anything, do an INSERT.
  701. if pk_set and not force_insert:
  702. base_qs = cls._base_manager.using(using)
  703. values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
  704. for f in non_pks]
  705. forced_update = update_fields or force_update
  706. updated = self._do_update(base_qs, using, pk_val, values, update_fields,
  707. forced_update)
  708. if force_update and not updated:
  709. raise DatabaseError("Forced update did not affect any rows.")
  710. if update_fields and not updated:
  711. raise DatabaseError("Save with update_fields did not affect any rows.")
  712. if not updated:
  713. if meta.order_with_respect_to:
  714. # If this is a model with an order_with_respect_to
  715. # autopopulate the _order field
  716. field = meta.order_with_respect_to
  717. filter_args = field.get_filter_kwargs_for_object(self)
  718. order_value = cls._base_manager.using(using).filter(**filter_args).count()
  719. self._order = order_value
  720. fields = meta.local_concrete_fields
  721. if not pk_set:
  722. fields = [f for f in fields if f is not meta.auto_field]
  723. update_pk = meta.auto_field and not pk_set
  724. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  725. if update_pk:
  726. setattr(self, meta.pk.attname, result)
  727. return updated
  728. def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
  729. """
  730. Try to update the model. Return True if the model was updated (if an
  731. update query was done and a matching row was found in the DB).
  732. """
  733. filtered = base_qs.filter(pk=pk_val)
  734. if not values:
  735. # We can end up here when saving a model in inheritance chain where
  736. # update_fields doesn't target any field in current model. In that
  737. # case we just say the update succeeded. Another case ending up here
  738. # is a model with just PK - in that case check that the PK still
  739. # exists.
  740. return update_fields is not None or filtered.exists()
  741. if self._meta.select_on_save and not forced_update:
  742. return (
  743. filtered.exists() and
  744. # It may happen that the object is deleted from the DB right after
  745. # this check, causing the subsequent UPDATE to return zero matching
  746. # rows. The same result can occur in some rare cases when the
  747. # database returns zero despite the UPDATE being executed
  748. # successfully (a row is matched and updated). In order to
  749. # distinguish these two cases, the object's existence in the
  750. # database is again checked for if the UPDATE query returns 0.
  751. (filtered._update(values) > 0 or filtered.exists())
  752. )
  753. return filtered._update(values) > 0
  754. def _do_insert(self, manager, using, fields, update_pk, raw):
  755. """
  756. Do an INSERT. If update_pk is defined then this method should return
  757. the new pk for the model.
  758. """
  759. return manager._insert([self], fields=fields, return_id=update_pk,
  760. using=using, raw=raw)
  761. def delete(self, using=None, keep_parents=False):
  762. using = using or router.db_for_write(self.__class__, instance=self)
  763. assert self.pk is not None, (
  764. "%s object can't be deleted because its %s attribute is set to None." %
  765. (self._meta.object_name, self._meta.pk.attname)
  766. )
  767. collector = Collector(using=using)
  768. collector.collect([self], keep_parents=keep_parents)
  769. return collector.delete()
  770. delete.alters_data = True
  771. def _get_FIELD_display(self, field):
  772. value = getattr(self, field.attname)
  773. return force_text(dict(field.flatchoices).get(value, value), strings_only=True)
  774. def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
  775. if not self.pk:
  776. raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
  777. op = 'gt' if is_next else 'lt'
  778. order = '' if is_next else '-'
  779. param = getattr(self, field.attname)
  780. q = Q(**{'%s__%s' % (field.name, op): param})
  781. q = q | Q(**{field.name: param, 'pk__%s' % op: self.pk})
  782. qs = self.__class__._default_manager.using(self._state.db).filter(**kwargs).filter(q).order_by(
  783. '%s%s' % (order, field.name), '%spk' % order
  784. )
  785. try:
  786. return qs[0]
  787. except IndexError:
  788. raise self.DoesNotExist("%s matching query does not exist." % self.__class__._meta.object_name)
  789. def _get_next_or_previous_in_order(self, is_next):
  790. cachename = "__%s_order_cache" % is_next
  791. if not hasattr(self, cachename):
  792. op = 'gt' if is_next else 'lt'
  793. order = '_order' if is_next else '-_order'
  794. order_field = self._meta.order_with_respect_to
  795. filter_args = order_field.get_filter_kwargs_for_object(self)
  796. obj = self.__class__._default_manager.filter(**filter_args).filter(**{
  797. '_order__%s' % op: self.__class__._default_manager.values('_order').filter(**{
  798. self._meta.pk.name: self.pk
  799. })
  800. }).order_by(order)[:1].get()
  801. setattr(self, cachename, obj)
  802. return getattr(self, cachename)
  803. def prepare_database_save(self, field):
  804. if self.pk is None:
  805. raise ValueError("Unsaved model instance %r cannot be used in an ORM query." % self)
  806. return getattr(self, field.remote_field.get_related_field().attname)
  807. def clean(self):
  808. """
  809. Hook for doing any extra model-wide validation after clean() has been
  810. called on every field by self.clean_fields. Any ValidationError raised
  811. by this method will not be associated with a particular field; it will
  812. have a special-case association with the field defined by NON_FIELD_ERRORS.
  813. """
  814. pass
  815. def validate_unique(self, exclude=None):
  816. """
  817. Check unique constraints on the model and raise ValidationError if any
  818. failed.
  819. """
  820. unique_checks, date_checks = self._get_unique_checks(exclude=exclude)
  821. errors = self._perform_unique_checks(unique_checks)
  822. date_errors = self._perform_date_checks(date_checks)
  823. for k, v in date_errors.items():
  824. errors.setdefault(k, []).extend(v)
  825. if errors:
  826. raise ValidationError(errors)
  827. def _get_unique_checks(self, exclude=None):
  828. """
  829. Return a list of checks to perform. Since validate_unique() could be
  830. called from a ModelForm, some fields may have been excluded; we can't
  831. perform a unique check on a model that is missing fields involved
  832. in that check. Fields that did not validate should also be excluded,
  833. but they need to be passed in via the exclude argument.
  834. """
  835. if exclude is None:
  836. exclude = []
  837. unique_checks = []
  838. unique_togethers = [(self.__class__, self._meta.unique_together)]
  839. for parent_class in self._meta.get_parent_list():
  840. if parent_class._meta.unique_together:
  841. unique_togethers.append((parent_class, parent_class._meta.unique_together))
  842. for model_class, unique_together in unique_togethers:
  843. for check in unique_together:
  844. if not any(name in exclude for name in check):
  845. # Add the check if the field isn't excluded.
  846. unique_checks.append((model_class, tuple(check)))
  847. # These are checks for the unique_for_<date/year/month>.
  848. date_checks = []
  849. # Gather a list of checks for fields declared as unique and add them to
  850. # the list of checks.
  851. fields_with_class = [(self.__class__, self._meta.local_fields)]
  852. for parent_class in self._meta.get_parent_list():
  853. fields_with_class.append((parent_class, parent_class._meta.local_fields))
  854. for model_class, fields in fields_with_class:
  855. for f in fields:
  856. name = f.name
  857. if name in exclude:
  858. continue
  859. if f.unique:
  860. unique_checks.append((model_class, (name,)))
  861. if f.unique_for_date and f.unique_for_date not in exclude:
  862. date_checks.append((model_class, 'date', name, f.unique_for_date))
  863. if f.unique_for_year and f.unique_for_year not in exclude:
  864. date_checks.append((model_class, 'year', name, f.unique_for_year))
  865. if f.unique_for_month and f.unique_for_month not in exclude:
  866. date_checks.append((model_class, 'month', name, f.unique_for_month))
  867. return unique_checks, date_checks
  868. def _perform_unique_checks(self, unique_checks):
  869. errors = {}
  870. for model_class, unique_check in unique_checks:
  871. # Try to look up an existing object with the same values as this
  872. # object's values for all the unique field.
  873. lookup_kwargs = {}
  874. for field_name in unique_check:
  875. f = self._meta.get_field(field_name)
  876. lookup_value = getattr(self, f.attname)
  877. # TODO: Handle multiple backends with different feature flags.
  878. if (lookup_value is None or
  879. (lookup_value == '' and connection.features.interprets_empty_strings_as_nulls)):
  880. # no value, skip the lookup
  881. continue
  882. if f.primary_key and not self._state.adding:
  883. # no need to check for unique primary key when editing
  884. continue
  885. lookup_kwargs[str(field_name)] = lookup_value
  886. # some fields were skipped, no reason to do the check
  887. if len(unique_check) != len(lookup_kwargs):
  888. continue
  889. qs = model_class._default_manager.filter(**lookup_kwargs)
  890. # Exclude the current object from the query if we are editing an
  891. # instance (as opposed to creating a new one)
  892. # Note that we need to use the pk as defined by model_class, not
  893. # self.pk. These can be different fields because model inheritance
  894. # allows single model to have effectively multiple primary keys.
  895. # Refs #17615.
  896. model_class_pk = self._get_pk_val(model_class._meta)
  897. if not self._state.adding and model_class_pk is not None:
  898. qs = qs.exclude(pk=model_class_pk)
  899. if qs.exists():
  900. if len(unique_check) == 1:
  901. key = unique_check[0]
  902. else:
  903. key = NON_FIELD_ERRORS
  904. errors.setdefault(key, []).append(self.unique_error_message(model_class, unique_check))
  905. return errors
  906. def _perform_date_checks(self, date_checks):
  907. errors = {}
  908. for model_class, lookup_type, field, unique_for in date_checks:
  909. lookup_kwargs = {}
  910. # there's a ticket to add a date lookup, we can remove this special
  911. # case if that makes it's way in
  912. date = getattr(self, unique_for)
  913. if date is None:
  914. continue
  915. if lookup_type == 'date':
  916. lookup_kwargs['%s__day' % unique_for] = date.day
  917. lookup_kwargs['%s__month' % unique_for] = date.month
  918. lookup_kwargs['%s__year' % unique_for] = date.year
  919. else:
  920. lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(date, lookup_type)
  921. lookup_kwargs[field] = getattr(self, field)
  922. qs = model_class._default_manager.filter(**lookup_kwargs)
  923. # Exclude the current object from the query if we are editing an
  924. # instance (as opposed to creating a new one)
  925. if not self._state.adding and self.pk is not None:
  926. qs = qs.exclude(pk=self.pk)
  927. if qs.exists():
  928. errors.setdefault(field, []).append(
  929. self.date_error_message(lookup_type, field, unique_for)
  930. )
  931. return errors
  932. def date_error_message(self, lookup_type, field_name, unique_for):
  933. opts = self._meta
  934. field = opts.get_field(field_name)
  935. return ValidationError(
  936. message=field.error_messages['unique_for_date'],
  937. code='unique_for_date',
  938. params={
  939. 'model': self,
  940. 'model_name': capfirst(opts.verbose_name),
  941. 'lookup_type': lookup_type,
  942. 'field': field_name,
  943. 'field_label': capfirst(field.verbose_name),
  944. 'date_field': unique_for,
  945. 'date_field_label': capfirst(opts.get_field(unique_for).verbose_name),
  946. }
  947. )
  948. def unique_error_message(self, model_class, unique_check):
  949. opts = model_class._meta
  950. params = {
  951. 'model': self,
  952. 'model_class': model_class,
  953. 'model_name': capfirst(opts.verbose_name),
  954. 'unique_check': unique_check,
  955. }
  956. # A unique field
  957. if len(unique_check) == 1:
  958. field = opts.get_field(unique_check[0])
  959. params['field_label'] = capfirst(field.verbose_name)
  960. return ValidationError(
  961. message=field.error_messages['unique'],
  962. code='unique',
  963. params=params,
  964. )
  965. # unique_together
  966. else:
  967. field_labels = [capfirst(opts.get_field(f).verbose_name) for f in unique_check]
  968. params['field_labels'] = get_text_list(field_labels, _('and'))
  969. return ValidationError(
  970. message=_("%(model_name)s with this %(field_labels)s already exists."),
  971. code='unique_together',
  972. params=params,
  973. )
  974. def full_clean(self, exclude=None, validate_unique=True):
  975. """
  976. Call clean_fields(), clean(), and validate_unique() on the model.
  977. Raise a ValidationError for any errors that occur.
  978. """
  979. errors = {}
  980. if exclude is None:
  981. exclude = []
  982. else:
  983. exclude = list(exclude)
  984. try:
  985. self.clean_fields(exclude=exclude)
  986. except ValidationError as e:
  987. errors = e.update_error_dict(errors)
  988. # Form.clean() is run even if other validation fails, so do the
  989. # same with Model.clean() for consistency.
  990. try:
  991. self.clean()
  992. except ValidationError as e:
  993. errors = e.update_error_dict(errors)
  994. # Run unique checks, but only for fields that passed validation.
  995. if validate_unique:
  996. for name in errors:
  997. if name != NON_FIELD_ERRORS and name not in exclude:
  998. exclude.append(name)
  999. try:
  1000. self.validate_unique(exclude=exclude)
  1001. except ValidationError as e:
  1002. errors = e.update_error_dict(errors)
  1003. if errors:
  1004. raise ValidationError(errors)
  1005. def clean_fields(self, exclude=None):
  1006. """
  1007. Clean all fields and raise a ValidationError containing a dict
  1008. of all validation errors if any occur.
  1009. """
  1010. if exclude is None:
  1011. exclude = []
  1012. errors = {}
  1013. for f in self._meta.fields:
  1014. if f.name in exclude:
  1015. continue
  1016. # Skip validation for empty fields with blank=True. The developer
  1017. # is responsible for making sure they have a valid value.
  1018. raw_value = getattr(self, f.attname)
  1019. if f.blank and raw_value in f.empty_values:
  1020. continue
  1021. try:
  1022. setattr(self, f.attname, f.clean(raw_value, self))
  1023. except ValidationError as e:
  1024. errors[f.name] = e.error_list
  1025. if errors:
  1026. raise ValidationError(errors)
  1027. @classmethod
  1028. def check(cls, **kwargs):
  1029. errors = [*cls._check_swappable(), *cls._check_model(), *cls._check_managers(**kwargs)]
  1030. if not cls._meta.swapped:
  1031. errors += [
  1032. *cls._check_fields(**kwargs),
  1033. *cls._check_m2m_through_same_relationship(),
  1034. *cls._check_long_column_names(),
  1035. ]
  1036. clash_errors = (
  1037. *cls._check_id_field(),
  1038. *cls._check_field_name_clashes(),
  1039. *cls._check_model_name_db_lookup_clashes(),
  1040. *cls._check_property_name_related_field_accessor_clashes(),
  1041. *cls._check_single_primary_key(),
  1042. )
  1043. errors.extend(clash_errors)
  1044. # If there are field name clashes, hide consequent column name
  1045. # clashes.
  1046. if not clash_errors:
  1047. errors.extend(cls._check_column_name_clashes())
  1048. errors += [
  1049. *cls._check_index_together(),
  1050. *cls._check_unique_together(),
  1051. *cls._check_indexes(),
  1052. *cls._check_ordering(),
  1053. ]
  1054. return errors
  1055. @classmethod
  1056. def _check_swappable(cls):
  1057. """Check if the swapped model exists."""
  1058. errors = []
  1059. if cls._meta.swapped:
  1060. try:
  1061. apps.get_model(cls._meta.swapped)
  1062. except ValueError:
  1063. errors.append(
  1064. checks.Error(
  1065. "'%s' is not of the form 'app_label.app_name'." % cls._meta.swappable,
  1066. id='models.E001',
  1067. )
  1068. )
  1069. except LookupError:
  1070. app_label, model_name = cls._meta.swapped.split('.')
  1071. errors.append(
  1072. checks.Error(
  1073. "'%s' references '%s.%s', which has not been "
  1074. "installed, or is abstract." % (
  1075. cls._meta.swappable, app_label, model_name
  1076. ),
  1077. id='models.E002',
  1078. )
  1079. )
  1080. return errors
  1081. @classmethod
  1082. def _check_model(cls):
  1083. errors = []
  1084. if cls._meta.proxy:
  1085. if cls._meta.local_fields or cls._meta.local_many_to_many:
  1086. errors.append(
  1087. checks.Error(
  1088. "Proxy model '%s' contains model fields." % cls.__name__,
  1089. id='models.E017',
  1090. )
  1091. )
  1092. return errors
  1093. @classmethod
  1094. def _check_managers(cls, **kwargs):
  1095. """Perform all manager checks."""
  1096. errors = []
  1097. for manager in cls._meta.managers:
  1098. errors.extend(manager.check(**kwargs))
  1099. return errors
  1100. @classmethod
  1101. def _check_fields(cls, **kwargs):
  1102. """Perform all field checks."""
  1103. errors = []
  1104. for field in cls._meta.local_fields:
  1105. errors.extend(field.check(**kwargs))
  1106. for field in cls._meta.local_many_to_many:
  1107. errors.extend(field.check(from_model=cls, **kwargs))
  1108. return errors
  1109. @classmethod
  1110. def _check_m2m_through_same_relationship(cls):
  1111. """ Check if no relationship model is used by more than one m2m field.
  1112. """
  1113. errors = []
  1114. seen_intermediary_signatures = []
  1115. fields = cls._meta.local_many_to_many
  1116. # Skip when the target model wasn't found.
  1117. fields = (f for f in fields if isinstance(f.remote_field.model, ModelBase))
  1118. # Skip when the relationship model wasn't found.
  1119. fields = (f for f in fields if isinstance(f.remote_field.through, ModelBase))
  1120. for f in fields:
  1121. signature = (f.remote_field.model, cls, f.remote_field.through)
  1122. if signature in seen_intermediary_signatures:
  1123. errors.append(
  1124. checks.Error(
  1125. "The model has two many-to-many relations through "
  1126. "the intermediate model '%s'." % f.remote_field.through._meta.label,
  1127. obj=cls,
  1128. id='models.E003',
  1129. )
  1130. )
  1131. else:
  1132. seen_intermediary_signatures.append(signature)
  1133. return errors
  1134. @classmethod
  1135. def _check_id_field(cls):
  1136. """Check if `id` field is a primary key."""
  1137. fields = [f for f in cls._meta.local_fields if f.name == 'id' and f != cls._meta.pk]
  1138. # fields is empty or consists of the invalid "id" field
  1139. if fields and not fields[0].primary_key and cls._meta.pk.name == 'id':
  1140. return [
  1141. checks.Error(
  1142. "'id' can only be used as a field name if the field also "
  1143. "sets 'primary_key=True'.",
  1144. obj=cls,
  1145. id='models.E004',
  1146. )
  1147. ]
  1148. else:
  1149. return []
  1150. @classmethod
  1151. def _check_field_name_clashes(cls):
  1152. """Forbid field shadowing in multi-table inheritance."""
  1153. errors = []
  1154. used_fields = {} # name or attname -> field
  1155. # Check that multi-inheritance doesn't cause field name shadowing.
  1156. for parent in cls._meta.get_parent_list():
  1157. for f in parent._meta.local_fields:
  1158. clash = used_fields.get(f.name) or used_fields.get(f.attname) or None
  1159. if clash:
  1160. errors.append(
  1161. checks.Error(
  1162. "The field '%s' from parent model "
  1163. "'%s' clashes with the field '%s' "
  1164. "from parent model '%s'." % (
  1165. clash.name, clash.model._meta,
  1166. f.name, f.model._meta
  1167. ),
  1168. obj=cls,
  1169. id='models.E005',
  1170. )
  1171. )
  1172. used_fields[f.name] = f
  1173. used_fields[f.attname] = f
  1174. # Check that fields defined in the model don't clash with fields from
  1175. # parents, including auto-generated fields like multi-table inheritance
  1176. # child accessors.
  1177. for parent in cls._meta.get_parent_list():
  1178. for f in parent._meta.get_fields():
  1179. if f not in used_fields:
  1180. used_fields[f.name] = f
  1181. for f in cls._meta.local_fields:
  1182. clash = used_fields.get(f.name) or used_fields.get(f.attname) or None
  1183. # Note that we may detect clash between user-defined non-unique
  1184. # field "id" and automatically added unique field "id", both
  1185. # defined at the same model. This special case is considered in
  1186. # _check_id_field and here we ignore it.
  1187. id_conflict = f.name == "id" and clash and clash.name == "id" and clash.model == cls
  1188. if clash and not id_conflict:
  1189. errors.append(
  1190. checks.Error(
  1191. "The field '%s' clashes with the field '%s' "
  1192. "from model '%s'." % (
  1193. f.name, clash.name, clash.model._meta
  1194. ),
  1195. obj=f,
  1196. id='models.E006',
  1197. )
  1198. )
  1199. used_fields[f.name] = f
  1200. used_fields[f.attname] = f
  1201. return errors
  1202. @classmethod
  1203. def _check_column_name_clashes(cls):
  1204. # Store a list of column names which have already been used by other fields.
  1205. used_column_names = []
  1206. errors = []
  1207. for f in cls._meta.local_fields:
  1208. _, column_name = f.get_attname_column()
  1209. # Ensure the column name is not already in use.
  1210. if column_name and column_name in used_column_names:
  1211. errors.append(
  1212. checks.Error(
  1213. "Field '%s' has column name '%s' that is used by "
  1214. "another field." % (f.name, column_name),
  1215. hint="Specify a 'db_column' for the field.",
  1216. obj=cls,
  1217. id='models.E007'
  1218. )
  1219. )
  1220. else:
  1221. used_column_names.append(column_name)
  1222. return errors
  1223. @classmethod
  1224. def _check_model_name_db_lookup_clashes(cls):
  1225. errors = []
  1226. model_name = cls.__name__
  1227. if model_name.startswith('_') or model_name.endswith('_'):
  1228. errors.append(
  1229. checks.Error(
  1230. "The model name '%s' cannot start or end with an underscore "
  1231. "as it collides with the query lookup syntax." % model_name,
  1232. obj=cls,
  1233. id='models.E023'
  1234. )
  1235. )
  1236. elif LOOKUP_SEP in model_name:
  1237. errors.append(
  1238. checks.Error(
  1239. "The model name '%s' cannot contain double underscores as "
  1240. "it collides with the query lookup syntax." % model_name,
  1241. obj=cls,
  1242. id='models.E024'
  1243. )
  1244. )
  1245. return errors
  1246. @classmethod
  1247. def _check_property_name_related_field_accessor_clashes(cls):
  1248. errors = []
  1249. property_names = cls._meta._property_names
  1250. related_field_accessors = (
  1251. f.get_attname() for f in cls._meta._get_fields(reverse=False)
  1252. if f.is_relation and f.related_model is not None
  1253. )
  1254. for accessor in related_field_accessors:
  1255. if accessor in property_names:
  1256. errors.append(
  1257. checks.Error(
  1258. "The property '%s' clashes with a related field "
  1259. "accessor." % accessor,
  1260. obj=cls,
  1261. id='models.E025',
  1262. )
  1263. )
  1264. return errors
  1265. @classmethod
  1266. def _check_single_primary_key(cls):
  1267. errors = []
  1268. if sum(1 for f in cls._meta.local_fields if f.primary_key) > 1:
  1269. errors.append(
  1270. checks.Error(
  1271. "The model cannot have more than one field with "
  1272. "'primary_key=True'.",
  1273. obj=cls,
  1274. id='models.E026',
  1275. )
  1276. )
  1277. return errors
  1278. @classmethod
  1279. def _check_index_together(cls):
  1280. """Check the value of "index_together" option."""
  1281. if not isinstance(cls._meta.index_together, (tuple, list)):
  1282. return [
  1283. checks.Error(
  1284. "'index_together' must be a list or tuple.",
  1285. obj=cls,
  1286. id='models.E008',
  1287. )
  1288. ]
  1289. elif any(not isinstance(fields, (tuple, list)) for fields in cls._meta.index_together):
  1290. return [
  1291. checks.Error(
  1292. "All 'index_together' elements must be lists or tuples.",
  1293. obj=cls,
  1294. id='models.E009',
  1295. )
  1296. ]
  1297. else:
  1298. errors = []
  1299. for fields in cls._meta.index_together:
  1300. errors.extend(cls._check_local_fields(fields, "index_together"))
  1301. return errors
  1302. @classmethod
  1303. def _check_unique_together(cls):
  1304. """Check the value of "unique_together" option."""
  1305. if not isinstance(cls._meta.unique_together, (tuple, list)):
  1306. return [
  1307. checks.Error(
  1308. "'unique_together' must be a list or tuple.",
  1309. obj=cls,
  1310. id='models.E010',
  1311. )
  1312. ]
  1313. elif any(not isinstance(fields, (tuple, list)) for fields in cls._meta.unique_together):
  1314. return [
  1315. checks.Error(
  1316. "All 'unique_together' elements must be lists or tuples.",
  1317. obj=cls,
  1318. id='models.E011',
  1319. )
  1320. ]
  1321. else:
  1322. errors = []
  1323. for fields in cls._meta.unique_together:
  1324. errors.extend(cls._check_local_fields(fields, "unique_together"))
  1325. return errors
  1326. @classmethod
  1327. def _check_indexes(cls):
  1328. """Check the fields of indexes."""
  1329. fields = [field for index in cls._meta.indexes for field, _ in index.fields_orders]
  1330. return cls._check_local_fields(fields, 'indexes')
  1331. @classmethod
  1332. def _check_local_fields(cls, fields, option):
  1333. from django.db import models
  1334. # In order to avoid hitting the relation tree prematurely, we use our
  1335. # own fields_map instead of using get_field()
  1336. forward_fields_map = {
  1337. field.name: field for field in cls._meta._get_fields(reverse=False)
  1338. }
  1339. errors = []
  1340. for field_name in fields:
  1341. try:
  1342. field = forward_fields_map[field_name]
  1343. except KeyError:
  1344. errors.append(
  1345. checks.Error(
  1346. "'%s' refers to the nonexistent field '%s'." % (
  1347. option, field_name,
  1348. ),
  1349. obj=cls,
  1350. id='models.E012',
  1351. )
  1352. )
  1353. else:
  1354. if isinstance(field.remote_field, models.ManyToManyRel):
  1355. errors.append(
  1356. checks.Error(
  1357. "'%s' refers to a ManyToManyField '%s', but "
  1358. "ManyToManyFields are not permitted in '%s'." % (
  1359. option, field_name, option,
  1360. ),
  1361. obj=cls,
  1362. id='models.E013',
  1363. )
  1364. )
  1365. elif field not in cls._meta.local_fields:
  1366. errors.append(
  1367. checks.Error(
  1368. "'%s' refers to field '%s' which is not local to model '%s'."
  1369. % (option, field_name, cls._meta.object_name),
  1370. hint="This issue may be caused by multi-table inheritance.",
  1371. obj=cls,
  1372. id='models.E016',
  1373. )
  1374. )
  1375. return errors
  1376. @classmethod
  1377. def _check_ordering(cls):
  1378. """
  1379. Check "ordering" option -- is it a list of strings and do all fields
  1380. exist?
  1381. """
  1382. if cls._meta._ordering_clash:
  1383. return [
  1384. checks.Error(
  1385. "'ordering' and 'order_with_respect_to' cannot be used together.",
  1386. obj=cls,
  1387. id='models.E021',
  1388. ),
  1389. ]
  1390. if cls._meta.order_with_respect_to or not cls._meta.ordering:
  1391. return []
  1392. if not isinstance(cls._meta.ordering, (list, tuple)):
  1393. return [
  1394. checks.Error(
  1395. "'ordering' must be a tuple or list (even if you want to order by only one field).",
  1396. obj=cls,
  1397. id='models.E014',
  1398. )
  1399. ]
  1400. errors = []
  1401. fields = cls._meta.ordering
  1402. # Skip expressions and '?' fields.
  1403. fields = (f for f in fields if isinstance(f, str) and f != '?')
  1404. # Convert "-field" to "field".
  1405. fields = ((f[1:] if f.startswith('-') else f) for f in fields)
  1406. # Skip ordering in the format field1__field2 (FIXME: checking
  1407. # this format would be nice, but it's a little fiddly).
  1408. fields = (f for f in fields if LOOKUP_SEP not in f)
  1409. # Skip ordering on pk. This is always a valid order_by field
  1410. # but is an alias and therefore won't be found by opts.get_field.
  1411. fields = {f for f in fields if f != 'pk'}
  1412. # Check for invalid or nonexistent fields in ordering.
  1413. invalid_fields = []
  1414. # Any field name that is not present in field_names does not exist.
  1415. # Also, ordering by m2m fields is not allowed.
  1416. opts = cls._meta
  1417. valid_fields = set(chain.from_iterable(
  1418. (f.name, f.attname) if not (f.auto_created and not f.concrete) else (f.field.related_query_name(),)
  1419. for f in chain(opts.fields, opts.related_objects)
  1420. ))
  1421. invalid_fields.extend(fields - valid_fields)
  1422. for invalid_field in invalid_fields:
  1423. errors.append(
  1424. checks.Error(
  1425. "'ordering' refers to the nonexistent field '%s'." % invalid_field,
  1426. obj=cls,
  1427. id='models.E015',
  1428. )
  1429. )
  1430. return errors
  1431. @classmethod
  1432. def _check_long_column_names(cls):
  1433. """
  1434. Check that any auto-generated column names are shorter than the limits
  1435. for each database in which the model will be created.
  1436. """
  1437. errors = []
  1438. allowed_len = None
  1439. db_alias = None
  1440. # Find the minimum max allowed length among all specified db_aliases.
  1441. for db in settings.DATABASES:
  1442. # skip databases where the model won't be created
  1443. if not router.allow_migrate_model(db, cls):
  1444. continue
  1445. connection = connections[db]
  1446. max_name_length = connection.ops.max_name_length()
  1447. if max_name_length is None or connection.features.truncates_names:
  1448. continue
  1449. else:
  1450. if allowed_len is None:
  1451. allowed_len = max_name_length
  1452. db_alias = db
  1453. elif max_name_length < allowed_len:
  1454. allowed_len = max_name_length
  1455. db_alias = db
  1456. if allowed_len is None:
  1457. return errors
  1458. for f in cls._meta.local_fields:
  1459. _, column_name = f.get_attname_column()
  1460. # Check if auto-generated name for the field is too long
  1461. # for the database.
  1462. if f.db_column is None and column_name is not None and len(column_name) > allowed_len:
  1463. errors.append(
  1464. checks.Error(
  1465. 'Autogenerated column name too long for field "%s". '
  1466. 'Maximum length is "%s" for database "%s".'
  1467. % (column_name, allowed_len, db_alias),
  1468. hint="Set the column name manually using 'db_column'.",
  1469. obj=cls,
  1470. id='models.E018',
  1471. )
  1472. )
  1473. for f in cls._meta.local_many_to_many:
  1474. # Skip nonexistent models.
  1475. if isinstance(f.remote_field.through, str):
  1476. continue
  1477. # Check if auto-generated name for the M2M field is too long
  1478. # for the database.
  1479. for m2m in f.remote_field.through._meta.local_fields:
  1480. _, rel_name = m2m.get_attname_column()
  1481. if m2m.db_column is None and rel_name is not None and len(rel_name) > allowed_len:
  1482. errors.append(
  1483. checks.Error(
  1484. 'Autogenerated column name too long for M2M field '
  1485. '"%s". Maximum length is "%s" for database "%s".'
  1486. % (rel_name, allowed_len, db_alias),
  1487. hint=(
  1488. "Use 'through' to create a separate model for "
  1489. "M2M and then set column_name using 'db_column'."
  1490. ),
  1491. obj=cls,
  1492. id='models.E019',
  1493. )
  1494. )
  1495. return errors
  1496. ############################################
  1497. # HELPER FUNCTIONS (CURRIED MODEL METHODS) #
  1498. ############################################
  1499. # ORDERING METHODS #########################
  1500. def method_set_order(self, ordered_obj, id_list, using=None):
  1501. if using is None:
  1502. using = DEFAULT_DB_ALIAS
  1503. order_wrt = ordered_obj._meta.order_with_respect_to
  1504. filter_args = order_wrt.get_forward_related_filter(self)
  1505. # FIXME: It would be nice if there was an "update many" version of update
  1506. # for situations like this.
  1507. with transaction.atomic(using=using, savepoint=False):
  1508. for i, j in enumerate(id_list):
  1509. ordered_obj.objects.filter(pk=j, **filter_args).update(_order=i)
  1510. def method_get_order(self, ordered_obj):
  1511. order_wrt = ordered_obj._meta.order_with_respect_to
  1512. filter_args = order_wrt.get_forward_related_filter(self)
  1513. pk_name = ordered_obj._meta.pk.name
  1514. return ordered_obj.objects.filter(**filter_args).values_list(pk_name, flat=True)
  1515. def make_foreign_order_accessors(model, related_model):
  1516. setattr(
  1517. related_model,
  1518. 'get_%s_order' % model.__name__.lower(),
  1519. partialmethod(method_get_order, model)
  1520. )
  1521. setattr(
  1522. related_model,
  1523. 'set_%s_order' % model.__name__.lower(),
  1524. partialmethod(method_set_order, model)
  1525. )
  1526. ########
  1527. # MISC #
  1528. ########
  1529. def model_unpickle(model_id):
  1530. """Used to unpickle Model subclasses with deferred fields."""
  1531. if isinstance(model_id, tuple):
  1532. model = apps.get_model(*model_id)
  1533. else:
  1534. # Backwards compat - the model was cached directly in earlier versions.
  1535. model = model_id
  1536. return model.__new__(model)
  1537. model_unpickle.__safe_for_unpickle__ = True