From 0e1d18507b484fa6c25a6d2ad45b001ac3122107 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 16 Mar 2026 13:22:52 -0400 Subject: [PATCH] INTPYTHON-935 Added NotSupportedError for QuerySet.extra(where=...) --- django_mongodb_backend/compiler.py | 4 ++-- django_mongodb_backend/query.py | 2 +- docs/releases/6.0.x.rst | 3 +++ tests/queries_/test_extra.py | 12 ++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/queries_/test_extra.py diff --git a/django_mongodb_backend/compiler.py b/django_mongodb_backend/compiler.py index 28b089031..2154ae21b 100644 --- a/django_mongodb_backend/compiler.py +++ b/django_mongodb_backend/compiler.py @@ -13,7 +13,7 @@ from django.db.models.sql import compiler from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, MULTI, SINGLE from django.db.models.sql.datastructures import BaseTable -from django.db.models.sql.where import AND, OR, XOR, NothingNode, WhereNode +from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode from django.utils.functional import cached_property from pymongo import ASCENDING, DESCENDING @@ -733,7 +733,7 @@ def _get_pushable_conditions(self): @classmethod def _collect_pushable(cls, expr, negated=False): - if expr is None or isinstance(expr, NothingNode): + if expr is None or isinstance(expr, (NothingNode, ExtraWhere)): return {} if isinstance(expr, WhereNode): # Apply De Morgan: track negation so connectors are flipped diff --git a/django_mongodb_backend/query.py b/django_mongodb_backend/query.py index 3679d185f..d4dc27b5c 100644 --- a/django_mongodb_backend/query.py +++ b/django_mongodb_backend/query.py @@ -166,7 +166,7 @@ def get_pipeline(self): return pipeline -def extra_where(self, compiler, connection): # noqa: ARG001 +def extra_where(self, compiler, connection, as_expr=False): # noqa: ARG001 raise NotSupportedError("QuerySet.extra() is not supported on MongoDB.") diff --git a/docs/releases/6.0.x.rst b/docs/releases/6.0.x.rst index e0b550caf..3c04e2463 100644 --- a/docs/releases/6.0.x.rst +++ b/docs/releases/6.0.x.rst @@ -35,6 +35,9 @@ Bug fixes that references in migrations don't need to be adjusted if models are later removed. +- Added a helpful ``NotSupportedError`` for ``QuerySet.extra(where=...)`` + rather than crashing with a more obscure exception. + Deprecated features ------------------- diff --git a/tests/queries_/test_extra.py b/tests/queries_/test_extra.py new file mode 100644 index 000000000..3c55e6c09 --- /dev/null +++ b/tests/queries_/test_extra.py @@ -0,0 +1,12 @@ +from django.db import NotSupportedError +from django.test import TestCase + +from .models import Author + + +class ExtraTests(TestCase): + def test_where(self): + author = Author.objects.create() + msg = "QuerySet.extra() is not supported on MongoDB." + with self.assertRaisesMessage(NotSupportedError, msg): + self.assertCountEqual(Author.objects.extra(where=["id=%s"], params=[author.id]), author)