Limit django query amounts in a certain scope
Project description
Ever wanted to limit how many db queries a certain function or view can make? You're in luck!
Get it from PyPi:
pip install django-query-limiter[formatting]
Basic usage
from your_django_models import Model
from query_limiter import limit_queries
with limit_queries(1):
print(Model.objects.first()) # Prints your model's first object
with limit_queries(1):
print(Model.objects.first()) # Prints your model's first object
print(Model.objects.first()) # Raises QueryLimitError
It can also be used as a function decorator, and can limit queries on a per-database level:
from your_django_models import Model
from query_limiter import limit_queries
@limit_queries(10, db_connections=['default'])
def my_view(request):
return Model.objects.first()
Multiple limits can be specified, and the first one to be reached will raise a QueryLimitExceeded exception:
from your_django_models import Model
from query_limiter import limit_queries
from datetime import timedelta
@limit_queries(
amount=2,
individual_max_time=timedelta(milliseconds=100),
total_max_time=timedelta(seconds=1),
)
def my_view(request):
Model.objects.first() # if this query takes more than 100ms -> QueryLimitExceeded
Model.objects.first() # if the total time of all queries takes more than 1s -> QueryLimitExceeded
Model.objects.first() # if this query is reached -> QueryLimitExceeded
You can globally disable the query limiter:
from your_django_models import Model
from query_limiter import limit_queries, disable_query_limiter
if settings.ENVIROMENT == 'production':
disable_query_limiter()
with limit_queries(1):
print(Model.objects.first()) # Prints your model's first object
print(Model.objects.first()) # Prints your model's first object
The default behaviour is to limit the connections to all databases. This can be globally overwriten like so:
from query_limiter import set_default_databases
set_default_databases(['your_database_name', 'another_database_name'])
Unless the db connections are specified in the limit_queries decorator, these databases will be used.
Formatting
The formatting extra:
- humanizes all timedelta outputs
- uses
sqlparseto format the queries to be more human readable
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-query-limiter-0.2.0.tar.gz.
File metadata
- Download URL: django-query-limiter-0.2.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a072558c69bff7fdf920655d5ad6fe36475c611fb6e03a34ce2f80051622a848
|
|
| MD5 |
483a2d85f60a90311f2d27029bb1a34b
|
|
| BLAKE2b-256 |
a41786e48cc0efde924a9777dc05abf68543e8c9283eaaab4dc065784f00fc2d
|
File details
Details for the file django_query_limiter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_query_limiter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
966953759ee95080cdb787937d51da1e14e9082338e0e45871262a3d2fa56211
|
|
| MD5 |
5c3a8eee4bbc4f3624aae81b73b34e1a
|
|
| BLAKE2b-256 |
eaa70847aa649593e173b450c750cd779a36d83f30f2c14b7f8fb72c4dc060a3
|