diff --git a/django/db/backends/base/base.py b/django/db/backends/base/base.py index 7a1dfd30d1..e6e0325d07 100644 --- a/django/db/backends/base/base.py +++ b/django/db/backends/base/base.py @@ -175,7 +175,8 @@ class BaseDatabaseWrapper: if len(self.queries_log) == self.queries_log.maxlen: warnings.warn( "Limit for query logging exceeded, only the last {} queries " - "will be returned.".format(self.queries_log.maxlen) + "will be returned.".format(self.queries_log.maxlen), + stacklevel=2, ) return list(self.queries_log) diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 4bab62aebb..2adfa51360 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -558,8 +558,9 @@ class BackendTestCase(TransactionTestCase): "Limit for query logging exceeded, only the last 3 queries will be " "returned." ) - with self.assertWarnsMessage(UserWarning, msg): + with self.assertWarnsMessage(UserWarning, msg) as ctx: self.assertEqual(3, len(new_connection.queries)) + self.assertEqual(ctx.filename, __file__) finally: BaseDatabaseWrapper.queries_limit = old_queries_limit