# Proposal to add assertNumQueriesLte function

**URL:** <https://forum.djangoproject.com/t/proposal-to-add-assertnumquerieslte-function/44099>\
**Category:** Django Internals\
**Created:** [February 6, 2026, 5:53pm UTC](https://forum.djangoproject.com/t/proposal-to-add-assertnumquerieslte-function/44099 "2026-02-06T17:53:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogenodisho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ogenodisho/32/27473_2.png) [@ogenodisho](https://forum.djangoproject.com/u/ogenodisho)\
**Post date:** [February 6, 2026, 5:53pm UTC](https://forum.djangoproject.com/t/proposal-to-add-assertnumquerieslte-function/44099/1 "2026-02-06T17:53:19Z")

</div>

I could be wrong here, but I feel like when developers are asserting the number of queries that a code block makes, their true intention is to prevent it from exceeding a threshold.

With that being said, I think it would be more suitable to have an`assertNumQueriesLte` function.

Or a more flexible version could be `assertNumQueriesBetween(min=None, max=None)`.

(The current`assertNumQueries`function can make tests annoyingly brittle - any optimization that reduces the number of queries requires you to update your tests, and sometimes that’s troublesome)

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [February 8, 2026, 11:57am UTC](https://forum.djangoproject.com/t/proposal-to-add-assertnumquerieslte-function/44099/2 "2026-02-08T11:57:42Z")

</div>

I agree that it’s underpowered as a testing tool, but adding range support feels like a weak approach to me: it allows you to skip updating tests when you deliberately optimise code, but it also means tests will no longer catch when you inadvertently remove queries.

I’ve developed two packages providing improved query assertion tools, the most recent covered here: [Django: Introducing inline-snapshot-django - Adam Johnson](https://adamj.eu/tech/2025/06/24/django-introducing-inline-snapshot-django/)

Perhaps that will solve your problem better than making a change in django?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 8, 2026, 2:41pm UTC](https://forum.djangoproject.com/t/proposal-to-add-assertnumquerieslte-function/44099/3 "2026-02-08T14:41:17Z")

</div>

I think there’s room for improvement around limiting the checks on the specific models that are generating queries as well. I created [GitHub - aspiredu/django-assert-model-queries: A utility to count the number of model queries in a Django application.](https://github.com/aspiredu/django-assert-model-queries) as an exploration into that, but haven’t done a ton with it.

Example from the docs:

```python
# Django TestCase example

from django.test import TestCase
from django_assert_model_queries import AssertModelQueries, ModelNumQueriesHelper
from testapp.models import Community

class TestDjangoIntegration(ModelNumQueriesHelper, TestCase):
    def test_assert_model_num_queries_context(self):
        with AssertModelQueries({"testapp.Community": 1}):
            Community.objects.create(name="test")
        with AssertModelQueries({"testapp.Community": 2, "testapp.Chapter": 1, "testapp.Community_topics": 1}):
            Community.objects.all().delete()

```
