Skip to main content

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

  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.

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

django_query_prefixer-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

django_query_prefixer-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django_query_prefixer-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_query_prefixer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8eaccea3288f5ff9ab26d86b1d70b3c77a91138663d84662e0c6a187cfd9f34e
MD5 a6158cf5bfdadf25666d0375cf264aa3
BLAKE2b-256 ce220c1a98bfb780d64b6377dfa084e926dd9136911935b75572d3ef01c44bd1

See more details on using hashes here.

File details

Details for the file django_query_prefixer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_query_prefixer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98edaaee5a402ccc89db7045b2fd1355aa9670ff2ccf12619cd063301d2e3da0
MD5 031681fcd3a627496d1217ab1aa84fd3
BLAKE2b-256 f1630d07ece0502501fb0c4e81e3b0abd14e48f4a09e34d7d9ddbf6bd39f7910

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page