MCPcopy
hub / github.com/django/django / get_prep_lookup

Method get_prep_lookup

django/db/models/fields/related_lookups.py:43–84  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

41
42class RelatedIn(In):
43 def get_prep_lookup(self):
44 from django.db.models.sql.query import Query # avoid circular import
45
46 if isinstance(self.lhs, ColPairs):
47 if (
48 isinstance(self.rhs, Query)
49 and not self.rhs.has_select_fields
50 and self.lhs.output_field.related_model is self.rhs.model
51 ):
52 self.rhs.set_values([f.name for f in self.lhs.sources])
53 else:
54 if self.rhs_is_direct_value():
55 # If we get here, we are dealing with single-column relations.
56 self.rhs = [get_normalized_value(val, self.lhs)[0] for val in self.rhs]
57 # We need to run the related field's get_prep_value(). Consider
58 # case ForeignKey to IntegerField given value 'abc'. The
59 # ForeignKey itself doesn't have validation for non-integers,
60 # so we must run validation using the target field.
61 if hasattr(self.lhs.output_field, "path_infos"):
62 # Run the target field's get_prep_value. We can safely
63 # assume there is only one as we don't get to the direct
64 # value branch otherwise.
65 target_field = self.lhs.output_field.path_infos[-1].target_fields[
66 -1
67 ]
68 self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
69 elif not getattr(self.rhs, "has_select_fields", True) and not getattr(
70 self.lhs.field.target_field, "primary_key", False
71 ):
72 if (
73 getattr(self.lhs.output_field, "primary_key", False)
74 and self.lhs.output_field.model == self.rhs.model
75 ):
76 # A case like
77 # Restaurant.objects.filter(place__in=restaurant_qs), where
78 # place is a OneToOneField and the primary key of
79 # Restaurant.
80 target_field = self.lhs.field.name
81 else:
82 target_field = self.lhs.field.target_field.name
83 self.rhs.set_values([target_field])
84 return super().get_prep_lookup()
85
86 def as_sql(self, compiler, connection):
87 if isinstance(self.lhs, ColPairs):

Callers 1

get_prep_lookupMethod · 0.45

Calls 4

get_normalized_valueFunction · 0.85
set_valuesMethod · 0.80
rhs_is_direct_valueMethod · 0.80
get_prep_valueMethod · 0.45

Tested by

no test coverage detected