Add helpful annotations to SQL queries.
Project description
django-query-prefixer
The Django Query Prefixer allows you to prepend annotations to every query executed within your Django project. This can be useful for monitoring. By adding custom prefixes to queries, you can gain insights into their origin, and can track their performance in a more organized manner.
Installation
You can install django-query-prefixer
using pip:
pip install django-query-prefixer
Usage
- Change database engine in your
settings.py
todjango_query_prefixer.backends.<database backend>
.
DATABASES = {
"default": {
"ENGINE": "django_query_prefixer.backends.postgresql",
"HOST": "127.0.0.1",
"NAME": "postgres",
"PASSWORD": "postgres",
"USER": "postgres",
}
}
- (Optional) Add
django_query_prefixer.middlewares.request_route
to theMIDDLEWARE
list. This middleware addsroute
andview_name
prefixes to SQL queries.
MIDDLEWARE = [
"django_query_prefixer.middlewares.request_route",
# ...
]
- Now, whenever queries are executed in your Django project, the configured prefixes will be automatically added to those queries. For example, a query like this:
User.objects.filter(username='bob')
will be executed as:
/* view_name=example route=/example */ SELECT ... FROM "auth_user" WHERE ("auth_user"."username" = 'bob')
You can add additional context to queries using the sql_prefixes
contextmanager:
from django_query_prefixer import sql_prefixes
with sql_prefixes(user_id=request.user.id, foo="bar"):
User.objects.filter(username='bob')
/* user_id=X foo=bar view_name=example route=/example */ SELECT ... FROM "auth_user" WHERE ("auth_user"."username" = 'bob')
Contributing
Contributions to django-query-prefixer
are welcome! If you find a bug, want to
add a new feature, or improve the documentation, please open an issue or submit
a pull request in the GitHub repository.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file django_query_prefixer-0.1.2.tar.gz
.
File metadata
- Download URL: django_query_prefixer-0.1.2.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.25.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab9974ead990edaaf9b51445fab516da9b56bccba103b843565621705f6c2a38 |
|
MD5 | d73f4bb5ffb77f32a5af6c59ae1fd46c |
|
BLAKE2b-256 | 95baf622b9cd5245d9574e1b9b5280cec4930dbf359f5399f48de4027a5699e0 |
File details
Details for the file django_query_prefixer-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: django_query_prefixer-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.25.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df55869168c649fc7fb7a5d78942879ee145858b4354140fd2f9397b4b8f35e7 |
|
MD5 | ccd9cd32cd73ecab0088ecd7a114b71c |
|
BLAKE2b-256 | 7631ce09e3b04648f74c18f88583737c278940619bdf29f83a2a46152d67398d |