Debian Codegraph @

django/db/models/base.py

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