Skip to main content

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

  1. Change database engine in your settings.py to django_query_prefixer.backends.<database backend>.
DATABASES = {
    "default": {
        "ENGINE": "django_query_prefixer.backends.postgresql",
        "HOST": "127.0.0.1",
        "NAME": "postgres",
        "PASSWORD": "postgres",
        "USER": "postgres",
    }
}
  1. (Optional) Add django_query_prefixer.middlewares.request_route to the MIDDLEWARE list. This middleware adds route and view_name prefixes to SQL queries.
MIDDLEWARE = [
    "django_query_prefixer.middlewares.request_route",
    # ... 
]
  1. 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.

Metadata

Release files for django-query-prefixer 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-query-prefixer 0.1.3
File Size Uploaded
django_query_prefixer-0.1.3.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-query-prefixer 0.1.3
File Interpreter ABI Platform
django_query_prefixer-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / django_query_prefixer-0.1.3.tar.gz

Download URL django_query_prefixer-0.1.3.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
02f6448e40cda6fafaad60eb9e540c899bb704400c1e6609d855b70f5813d22f
BLAKE2b-256 checksum
How to use checksums
be33e85be0da75225641677e18f878e1fdea902ec7f4770097dc359a39157a95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.2

Release files / django_query_prefixer-0.1.3-py3-none-any.whl

Download URL django_query_prefixer-0.1.3-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4921c026413ec6564316256dd453ddbad2007ce9ed2292f21d1965f51f11f846
BLAKE2b-256 checksum
How to use checksums
04375b554f328b486fb9715e875e407d58802d14bbafe91b53e1f58290bf0a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.2

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page