django-mock-queries
A django library for mocking queryset functions in memory for testing
What it is and what it does
django-mock-queries lets you test Django code that queries models without setting up a database. It provides in-memory MockSet and MockModel classes that mimic Django's QuerySet API, supporting method chaining, filtering with Q objects, aggregates, field lookups, and CRUD operations. This is useful when you want fast unit tests that don't require database setup or migrations.
The library is typically used by patching Django model managers in test cases, allowing you to verify query logic in isolation. It also includes utilities for testing django-rest-framework serializers and can be configured to run an entire test suite without a database by monkey-patching the test database backend, which can significantly speed up test execution for projects with many migrations.
Use it for:
- Unit test Django view logic that queries models without setting up a test database
- Mock User.objects or other model managers in test patches to verify filter and query logic
- Test django-rest-framework serializers with mocked model instances and nested serializers
- Speed up test suites by running tests without database setup when database-free testing is feasible
- Verify QuerySet method chaining and complex filter combinations with Q objects in isolation
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Mocks Django QuerySet operations in memory for unit testing, supporting filtering, aggregation, field lookups, and CRUD operations without a database.
Yes. This is a mature, actively maintained testing utility with no dependencies, permissive licensing, and clear use cases for accelerating Django unit tests. Install it if you want to test query logic without database overhead; skip it only if your test suite already has fast database fixtures or you need QuerySet methods not yet supported (annotate, reverse, none, extra, raw, bulk_create, in_bulk, as_manager).
Install
django-mock-queries on PyPI
pip
pip install django-mock-queriesuv
uv add django-mock-queriespoetry
poetry add django-mock-queriesInstalling django-mock-queries
Before you install
Low friction install with no runtime dependencies. Actively maintained with recent commits and stable production status since 2016.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions—suitable for commercial and open-source projects.
Quickstart
pip install django-mock-queries
from django_mock_queries.query import MockSet, MockModel
qs = MockSet(
MockModel(mock_name='john', email='john@gmail.com'),
MockModel(mock_name='bill', email='bill@gmail.com'),
)
result = [x for x in qs.all().filter(email__icontains='gmail.com')]
Verify before relying
- Whether all QuerySet methods used in production code are supported (TODO lists annotate, reverse, none, extra, raw, bulk_create, in_bulk, as_manager)
- Performance characteristics when mocking large datasets in memory
Package facts
| License | MIT (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 687 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 204,780/month — #9,601 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_mock_queries-2.3.0-py2.py3-none-any.whl
Keywords: django, orm, mocking, unit-testing, tdd
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
mock-alchemyProvides mock helpers for SQLAlchemy sessions…
permissive · top 15,000 on PyPI
djangorestframework-bulkAdds bulk create, update, and delete operations…
permissive · top 15,000 on PyPI
django-query-builderBuilds and executes complex SQL queries…
permissive · top 15,000 on PyPI
django-cacheopsAutomatic and manual ORM query caching for…
permissive · top 5,000 on PyPI
django-zen-queriesPrevents unintended database queries in Django…
permissive · top 15,000 on PyPI
djangorestframework-filtersExtends Django REST Framework with advanced…
permissive · top 15,000 on PyPI
django-user-sessionsReplaces Django's opaque session backend with…
permissive · top 15,000 on PyPI
alchemy-mockProvides mock helpers for SQLAlchemy sessions…
permissive · top 15,000 on PyPI
django-chunkatorIterates over large Django QuerySets in…
permissive · top 15,000 on PyPI
django-sql-utilsProvides simplified APIs for Django querysets…
permissive · top 15,000 on PyPI