diff --git a/django_mongodb/lookups.py b/django_mongodb/lookups.py index c651dd6a..7ce0aa50 100644 --- a/django_mongodb/lookups.py +++ b/django_mongodb/lookups.py @@ -1,4 +1,6 @@ +from django.core.exceptions import EmptyResultSet, FullResultSet from django.db import NotSupportedError +from django.db.models.expressions import Value from django.db.models.fields.related_lookups import In, MultiColSource, RelatedIn from django.db.models.lookups import ( BuiltinLookup, @@ -48,6 +50,14 @@ def in_(self, compiler, connection): def is_null(self, compiler, connection): if not isinstance(self.rhs, bool): raise ValueError("The QuerySet value for an isnull lookup must be True or False.") + if isinstance(self.lhs, Value): + if self.lhs.value is None or ( + self.lhs.value == "" and connection.features.interprets_empty_strings_as_nulls + ): + result_exception = FullResultSet if self.rhs else EmptyResultSet + else: + result_exception = EmptyResultSet if self.rhs else FullResultSet + raise result_exception lhs_mql = process_lhs(self, compiler, connection) return connection.mongo_operators["isnull"](lhs_mql, self.rhs)