MCPcopy
hub / github.com/django/django / build_lookup

Method build_lookup

django/db/models/sql/query.py:1414–1458  ·  view source on GitHub ↗

Try to extract transforms and lookup from given lhs. The lhs value is something that works like SQLExpression. The rhs value is what the lookup is going to compare against. The lookups is a list of names to extract using get_lookup() and get_transform().

(self, lookups, lhs, rhs)

Source from the content-addressed store, hash-verified

1412 self.check_filterable(expr)
1413
1414 def build_lookup(self, lookups, lhs, rhs):
1415 """
1416 Try to extract transforms and lookup from given lhs.
1417
1418 The lhs value is something that works like SQLExpression.
1419 The rhs value is what the lookup is going to compare against.
1420 The lookups is a list of names to extract using get_lookup()
1421 and get_transform().
1422 """
1423 # __exact is the default lookup if one isn't given.
1424 *transforms, lookup_name = lookups or ["exact"]
1425 for name in transforms:
1426 lhs = self.try_transform(lhs, name, lookups)
1427 # First try get_lookup() so that the lookup takes precedence if the lhs
1428 # supports both transform and lookup for the name.
1429 lookup_class = lhs.get_lookup(lookup_name)
1430 if not lookup_class:
1431 # A lookup wasn't found. Try to interpret the name as a transform
1432 # and do an Exact lookup against it.
1433 lhs = self.try_transform(lhs, lookup_name)
1434 lookup_name = "exact"
1435 lookup_class = lhs.get_lookup(lookup_name)
1436 if not lookup_class:
1437 return
1438
1439 lookup = lookup_class(lhs, rhs)
1440 # Interpret '__exact=None' as the sql 'is NULL'; otherwise, reject all
1441 # uses of None as a query value unless the lookup supports it.
1442 if lookup.rhs is None and not lookup.can_use_none_as_rhs:
1443 if lookup_name not in ("exact", "iexact"):
1444 raise ValueError("Cannot use None as a query value")
1445 return lhs.get_lookup("isnull")(lhs, True)
1446
1447 # For Oracle '' is equivalent to null. The check must be done at this
1448 # stage because join promotion can't be done in the compiler. Using
1449 # DEFAULT_DB_ALIAS isn't nice but it's the best that can be done here.
1450 # A similar thing is done in is_nullable(), too.
1451 if (
1452 lookup_name in ("exact", "iexact")
1453 and lookup.rhs == ""
1454 and connections[DEFAULT_DB_ALIAS].features.interprets_empty_strings_as_nulls
1455 ):
1456 return lhs.get_lookup("isnull")(lhs, True)
1457
1458 return lookup
1459
1460 def try_transform(self, lhs, name, lookups=None):
1461 """

Callers 2

build_filterMethod · 0.95

Calls 2

try_transformMethod · 0.95
get_lookupMethod · 0.45

Tested by 1