{"id":15295178,"url":"https://github.com/surenkov/django-query-utils","last_synced_at":"2026-01-05T07:51:49.298Z","repository":{"id":45730163,"uuid":"514264920","full_name":"surenkov/django-query-utils","owner":"surenkov","description":"Hanful stuff to work with queries in Django","archived":false,"fork":false,"pushed_at":"2022-07-19T13:24:51.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-03T01:35:06.641Z","etag":null,"topics":["django","filters","postgresql","sql","utils"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/django-query-utils/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/surenkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-15T12:40:01.000Z","updated_at":"2023-03-07T04:55:31.000Z","dependencies_parsed_at":"2022-09-04T17:42:05.467Z","dependency_job_id":null,"html_url":"https://github.com/surenkov/django-query-utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surenkov%2Fdjango-query-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surenkov%2Fdjango-query-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surenkov%2Fdjango-query-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/surenkov%2Fdjango-query-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/surenkov","download_url":"https://codeload.github.com/surenkov/django-query-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245330907,"owners_count":20597865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","filters","postgresql","sql","utils"],"created_at":"2024-09-30T17:08:57.626Z","updated_at":"2026-01-05T07:51:49.262Z","avatar_url":"https://github.com/surenkov.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"![PyPI Version](https://img.shields.io/pypi/v/django-query-utils)\n\n## PostgreSQL locks\n\nDjango already exposes row-level locks with `QuerySet.select_for_update()` method.\nWhat's missing is table-level and advisory locks:\n\n``` python\nfrom django_query_utils.postgres.locks import table_lock, LockMode\n\nwith table_lock(\"auth_user\", \"auth_group\", mode=LockMode.Exclusive, timeout=10):\n    \"\"\" Perform some esclusive operations on locked tables \"\"\"\n\n\n# Set a lock for Django model tables\n\nfrom django.contrib.auth import models\n\nwith table_lock.for_model(models.User, models.Group, **kwargs):\n    ...\n```\n\n[Advisory locks support](https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS)\n\n``` python\nfrom django_query_utils.postgres.locks import advisory_lock\n\nlock_id = 42\n\nwith advisory_lock(lock_id, using=\"default\", nowait=True):\n    \"\"\" Perform some actions with locked resources \"\"\"\n\n\n# Create a more meaningful lock.\n# Postgres spports either a single `bigint` or (`int`, `int`) pair as a lock_id.\n# `advisory_lock` tries to convert any strings (or bigger ints) to postgres format\n# either with hashing and bit shifts.\n\nfrom django.db import transaction\nfrom django.contrib.auth.models import User\n\nuser = User.objects.get(id=42)\n\nwith transaction.atomic(), advisory_lock(\"user:act\", user.id, timeout=10):\n    \"\"\" Perform some actions with locked resources.\n        Timeout is only awailable within a transaction block. \"\"\"\n\n```\n\n\n## PostgreSQL full-text search support for `django-filter`\n\n``` python\nfrom django_query_utils.postgres.filters import FullTextSearchFilter\n\n\nclass MyFilterSet(django_filters.FilterSet):\n    search = FullTextSearchFilter(\n        vector=(\"field_1\", \"field__subfield\"),  # or `SearchVector` instance\n        search_type=\"phrase\",\n        config=\"french\",\n    )\n```\n\nWith `search_type=\"custom\"` you may pass custom query expressions\n\n``` python\n\nclass MyFilterSet(django_filters.FilterSet):\n    search = FullTextSearchFilter(\n        vector=(\"first_name\", \"last_name\", \"email\"),  # or `SearchVector` instance\n        search_type=\"custom\",\n    )\n\n\nqs = User.objects.all()\nfilters = MyFilterSet({\"search\": \"(John | Mike | Dan) Doe\"}, qs)\n```\n\n\n\n## Raw Query wrappers:\n\n``` python\nfrom django_query_utils import Query, query_context\n\nquery = Query(\"select first_name, last_name from auth_user where email = %(email)s\", {\"email\": \"jdoe@example.com\"})\n\nwith query_context(using=\"default\") as c:\n    results = list(c.execute(query))\n\nassert results == [{\"first_name\": \"John\", \"last_name\": \"Doe\"}]\n```\n\nDifferent result materializers:\n\n``` python\nfrom django_query_utils import PlainMaterializer, Query, query_context\n\nquery = Query(\n    \"select first_name, last_name from auth_user where email = %(email)s\",\n    {\"email\": \"jdoe@example.com\"},\n    materializer=PlainMaterializer(),\n)\n\nwith query_context(using=\"default\") as c:\n    results = list(c.execute(query))\n\nassert results == [(\"John\", \"Doe\")]\n\n```\n\nMore sophisticated transformers to kwarg classes:\n\n``` python\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass MyUser:\n    first_name: str\n    last_name: str\n\n\nwith query_context() as c:\n    results = c.execute(query.as_type(MyUser))\n    assert list(results) == [MyUser(first_name=\"John\", last_name=\"Doe\")]\n```\n\n(PostgreSQL only) `psycopg2.sql` query formatting support:\n\n``` python\nfrom psycopg2 import sql\n\nraw_q = sql.SQL(\"select first_name, last_name from auth_user where email = {}\")\nquery = Query(raw_q.format(sql.Literal(\"jdoe@example.com\")))\n\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurenkov%2Fdjango-query-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsurenkov%2Fdjango-query-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurenkov%2Fdjango-query-utils/lists"}