Skip to main content

Handful utils to work with raw queries in Django

Project description

PyPI Version

PostgreSQL locks

Django already exposes row-level locks with QuerySet.select_for_update() method. What's missing is table-level and advisory locks:

from django_query_utils.postgres.locks import table_lock, LockMode

with table_lock("auth_user", "auth_group", mode=LockMode.Exclusive, timeout=10):
    """ Perform some esclusive operations on locked tables """


# Set a lock for Django model tables

from django.contrib.auth import models

with table_lock.for_model(models.User, models.Group, **kwargs):
    ...

Advisory locks support

from django_query_utils.postgres.locks import advisory_lock

lock_id = 42

with advisory_lock(lock_id, using="default", nowait=True):
    """ Perform some actions with locked resources """


# Create a more meaningful lock.
# Postgres spports either a single `bigint` or (`int`, `int`) pair as a lock_id.
# `advisory_lock` tries to convert any strings (or bigger ints) to postgres format
# either with hashing and bit shifts.

from django.db import transaction
from django.contrib.auth.models import User

user = User.objects.get(id=42)

with transaction.atomic(), advisory_lock("user:act", user.id, timeout=10):
    """ Perform some actions with locked resources.
        Timeout is only awailable within a transaction block. """

PostgreSQL full-text search support for django-filter

from django_query_utils.postgres.filters import FullTextSearchFilter


class MyFilterSet(django_filters.FilterSet):
    search = FullTextSearchFilter(
        vector=("field_1", "field__subfield"),  # or `SearchVector` instance
        search_type="phrase",
        config="french",
    )

With search_type="custom" you may pass custom query expressions

class MyFilterSet(django_filters.FilterSet):
    search = FullTextSearchFilter(
        vector=("first_name", "last_name", "email"),  # or `SearchVector` instance
        search_type="custom",
    )


qs = User.objects.all()
filters = MyFilterSet({"search": "(John | Mike | Dan) Doe"}, qs)

Raw Query wrappers:

from django_query_utils import Query, query_context

query = Query("select first_name, last_name from auth_user where email = %(email)s", {"email": "jdoe@example.com"})

with query_context(using="default") as c:
    results = list(c.execute(query))

assert results == [{"first_name": "John", "last_name": "Doe"}]

Different result materializers:

from django_query_utils import PlainMaterializer, Query, query_context

query = Query(
    "select first_name, last_name from auth_user where email = %(email)s",
    {"email": "jdoe@example.com"},
    materializer=PlainMaterializer(),
)

with query_context(using="default") as c:
    results = list(c.execute(query))

assert results == [("John", "Doe")]

More sophisticated transformers to kwarg classes:

from dataclasses import dataclass


@dataclass
class MyUser:
    first_name: str
    last_name: str


with query_context() as c:
    results = c.execute(query.as_type(MyUser))
    assert list(results) == [MyUser(first_name="John", last_name="Doe")]

(PostgreSQL only) psycopg2.sql query formatting support:

from psycopg2 import sql

raw_q = sql.SQL("select first_name, last_name from auth_user where email = {}")
query = Query(raw_q.format(sql.Literal("jdoe@example.com")))

...

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-utils-0.0.5.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

django_query_utils-0.0.5-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file django-query-utils-0.0.5.tar.gz.

File metadata

  • Download URL: django-query-utils-0.0.5.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.3

File hashes

Hashes for django-query-utils-0.0.5.tar.gz
Algorithm Hash digest
SHA256 3dd33e7ccce5be9fa7669ef096a893ec23265e61bff6dbba1ef042be8c22db50
MD5 6bc2db71ad90d80cf5d1afbd7d7f9059
BLAKE2b-256 f0b43ec2311b957767f998105d39e5d5d8f7b759cf9311825de5112d94d18327

See more details on using hashes here.

File details

Details for the file django_query_utils-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_query_utils-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 97f34614f22cc303485f9edc4c73ba232941f6b2a6adfb50495d9a3f5d8fbceb
MD5 14f63d05884c7b1d3bf27695e66f913b
BLAKE2b-256 3ac389a4f65c22bf2b4bf1344f471b258f7ed419369c3f3236a020dc6616c5fc

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