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