MCPcopy
hub / github.com/django/django / _check_references

Method _check_references

django/db/models/constraints.py:77–115  ·  view source on GitHub ↗
(self, model, references)

Source from the content-addressed store, hash-verified

75 return []
76
77 def _check_references(self, model, references):
78 from django.db.models.fields.composite import CompositePrimaryKey
79
80 errors = []
81 fields = set()
82 for field_name, *lookups in references:
83 # pk is an alias that won't be found by opts.get_field().
84 if field_name != "pk" or isinstance(model._meta.pk, CompositePrimaryKey):
85 fields.add(field_name)
86 if not lookups:
87 # If it has no lookups it cannot result in a JOIN.
88 continue
89 try:
90 if field_name == "pk":
91 field = model._meta.pk
92 else:
93 field = model._meta.get_field(field_name)
94 if not field.is_relation or field.many_to_many or field.one_to_many:
95 continue
96 except FieldDoesNotExist:
97 continue
98 # JOIN must happen at the first lookup.
99 first_lookup = lookups[0]
100 if (
101 hasattr(field, "get_transform")
102 and hasattr(field, "get_lookup")
103 and field.get_transform(first_lookup) is None
104 and field.get_lookup(first_lookup) is None
105 ):
106 errors.append(
107 checks.Error(
108 "'constraints' refers to the joined field '%s'."
109 % LOOKUP_SEP.join([field_name, *lookups]),
110 obj=model,
111 id="models.E041",
112 )
113 )
114 errors.extend(model._check_local_fields(fields, "constraints"))
115 return errors
116
117 def deconstruct(self):
118 path = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)

Callers 3

checkMethod · 0.80
checkMethod · 0.80
checkMethod · 0.80

Calls 8

extendMethod · 0.80
_check_local_fieldsMethod · 0.80
addMethod · 0.45
get_fieldMethod · 0.45
get_transformMethod · 0.45
get_lookupMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected