base.py 68 KB

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