Skip to content

Commit

Permalink
Another try
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed May 13, 2022
1 parent 13c7cd1 commit d438338
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions django/db/models/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,21 @@ def as_sql(self, compiler, connection):
else:
return "%s IS NOT NULL" % sql, params

# TODO: This should be nicer
def as_postgresql(self, compiler, connection):
sql, params = self.as_sql(compiler, connection)
# TODO: What is the proper check here to be able to cast everything?
if not isinstance(self.rhs, bool):
raise ValueError(
"The QuerySet value for an isnull lookup must be True or False."
)
placeholder = "%s"
# TODO: What is the proper way to figure out the cast type?
if hasattr(self.lhs.field, "cast_db_type"):
cast_type = self.lhs.field.cast_db_type(connection)
sql = sql.replace("%s", "%%s::%s" % cast_type)
return sql, params
placeholder = "(%s)::" + self.lhs.field.cast_db_type(connection)
sql, params = compiler.compile(self.lhs)
if self.rhs:
return (placeholder + " IS NULL") % sql, params
else:
return (placeholder + " IS NOT NULL") % sql, params


@Field.register_lookup
Expand Down

0 comments on commit d438338

Please sign in to comment.