Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

DRF Query Lang

📖 Overview

DRF Query Lang is a lightweight query language for Django REST Framework. It allows API clients to dynamically define:

  • Which fields to fetch
  • Which relations to expand
  • Apply simple functions (count, min, max)

It automatically generates:

  • Optimized querysets (select_related, prefetch_related)
  • Dynamic DRF serializers

✨ Features

  • Strict DSL (Model{...} syntax)
  • Dynamic ModelSerializer generation
  • Automatic queryset optimizations
  • Configurable authorization & security rules
  • Ready-to-use DRF view (QueryLangView)

🚀 Installation

pip install drf-query-lang

Or with Poetry / pyproject.toml:

dependencies = [
    "drf-query-lang>=0.1.0"
]

⚙️ Configuration

In your Django settings.py:

DRF_QUERY_LANG = {
    "UNAUTHORIZED_MODELS": ["User"],  # forbidden models
    "UNAUTHORIZED_KEYS": ["password", "is_staff", "is_superuser"],  # forbidden fields
    "AUTHORIZATION_METHOD": "drf_query_lang.permission.base_permission",  # request -> bool
}

📡 Usage

1. Add endpoint

# urls.py
from django.urls import path, include

urlpatterns = [
    path("query-lang/", include("drf_query_lang.urls")),
]

2. Example queries

Simple fields

GET /query-lang/?query=Account{"nom","prenom"}

Response:

[
  {"nom": "Durand", "prenom": "Alice"},
  {"nom": "Martin", "prenom": "Bob"}
]

Relations

GET /query-lang/?query=User{"email", Profile:profile{"bio"}}

Response:

[
  {"email": "a@test.com", "profile": {"bio": "Hello"}},
  {"email": "b@test.com", "profile": {"bio": "World"}}
]

Functions

GET /query-lang/?query=User{"username", Post:posts:count()[{}]}

Response:

[
  {"username": "jdoe", "posts": 42},
  {"username": "jdupont", "posts": 7}
]

Existence check

GET /query-lang/?query=Group{"name"}&exist=true

Response:

{"exist": true}

🔒 Security

  • Blocked models: defined in UNAUTHORIZED_MODELS
  • Blocked fields: defined in UNAUTHORIZED_KEYS
  • Authorization: checked with AUTHORIZATION_METHOD(request)

🧪 Tests

Run the test suite:

pytest tests/

Example parser test:

def test_parse_simple_fields():
    query = 'Agent{"nom","prenom"}'
    parser = QueryLangParser(query)
    parsed = parser.parsed_data
    assert parsed["model"] == "Agent"
    assert "nom" in parsed["fields"]
    assert "prenom" in parsed["fields"]

📌 Limitations (v0.1 beta)

  • Only min, max, count functions supported
  • No pagination in the view yet
  • No automatic cycle detection (self-relations may cause infinite recursion)
  • Strict syntax: no spaces allowed in the top-level query Model {} will raise an error

📜 License

MIT © 2025

Metadata

Release files for drf-query-lang 0.0.1b1

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

Source distribution (sdist)

Source distribution for drf-query-lang 0.0.1b1
File Size Uploaded
drf_query_lang-0.0.1b1.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-query-lang 0.0.1b1
File Interpreter ABI Platform
drf_query_lang-0.0.1b1-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / drf_query_lang-0.0.1b1.tar.gz

Download URL drf_query_lang-0.0.1b1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1cdcd8f4599b2cd76448ff7e76bb630165aabbf9c06d02aa3a6d0f7c3143e7fa
BLAKE2b-256 checksum
How to use checksums
08f113ce7aaf5bfcbd03df58e29b8afb6c657f1e44e48523a7fc6d9adeda3fbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.2

Release files / drf_query_lang-0.0.1b1-py3-none-any.whl

Download URL drf_query_lang-0.0.1b1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d7ceef549206885f9ec22f0a1d41b4c897cbd3a490b257ccf2c1ea567521f30
BLAKE2b-256 checksum
How to use checksums
c3e78908b3fb72624e30fdc26b5c8624f3e94d796d3491594b8549bb60cedff8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.0.1b1 This release

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