Skip to main content

A dynamic ORM query builder for Django with NLP support

Project description

Django Dynamic Query

Secure AI-powered Dynamic Django ORM Query Builder

django-dynamic-query is a Django package that allows developers to safely generate Django ORM queries from natural language using AI models such as OpenAI or Ollama.

Instead of writing complex filtering logic manually, users can ask questions in plain English and the package converts them into validated Django ORM queries while protecting against unsafe operations.


Text Input AI Text Input AI


Visual Builder Visual Builder

Features

  • AI-powered natural language querying
  • Secure ORM query generation
  • Schema indexing using Django management command
  • OpenAI support
  • Ollama support
  • Built-in validation layer
  • Protection against dangerous ORM operations
  • Easy Django integration
  • Simple web dashboard
  • Extensible architecture

Installation

Install using pip:

pip install django-dynamic-query

or

pip install git+https://github.com/sajjad-ch/django-dynamic-query.git

Requirements

  • Python 3.10+
  • Django 4.2+

Add to INSTALLED_APPS

Open your project's settings.py

INSTALLED_APPS = [
    ...
    "django_dynamic_query",
]

Configuration Add the following configuration to your settings.py to enable and customize the AI-powered features:

DJANGO_DYNAMIC_QUERY = {
    # Set to True to enable AI processing (Natural Language to ORM)
    "ENABLE_NLP": True,
    
    # Choose 'local' for Ollama/Local LLMs or 'api' for OpenAI/Cloud services
    "MODEL_TYPE": "local", 
    
    # Required for 'local': The name of the model (e.g., 'gemma2', 'llama3')
    "MODEL_NAME": "llama3",
    
    # Optional for 'local': Absolute path to a model file if not using a registry
    "MODEL_PATH": None,
    
    # Required for 'api': Your API Key
    "AI_API_KEY": "sk-...",
}

Index Your Models

Before using AI queries, index your Django models.

Run:

python manage.py index_schema

This command scans your Django models and builds the schema used by the AI engine.

Whenever your models change, run the command again.


URLs

Add the package URLs to your project.

from django.urls import include, path

urlpatterns = [
    ...
    path(
        "dynamic-query/",
        include("django_dynamic_query.urls")
    ),
]

Dashboard

After adding the URLs, open:

http://localhost:8000/dynamic-query/dashboard

The dashboard allows testing natural language queries directly from the browser.


Basic Usage

Example:

from django_dynamic_query import ...

# Your code here

Natural language example:

Show all active users created this month.

The package converts the request into a validated Django ORM query.


Security

The package is designed with security in mind.

Validation includes:

  • Preventing dangerous ORM operations
  • Validating model names
  • Validating field names
  • Safe filter generation
  • Query sanitization

Supported AI Providers

Currently supported:

  • OpenAI
  • Ollama

The architecture allows adding additional providers in the future.


Project Structure

django_dynamic_query/

├── core/
│   ├── builder.py
│   ├── validators.py
│   └── exceptions.py
│
├── nlp/
│   ├── ai_handler.py
│   └── schema_rag.py
│
├── management/
│   └── commands/
│       └── index_schema.py
│
├── templates/
│
├── views.py
├── urls.py
└── utils.py

Running Tests

Run all tests:

python -m pytest

or

python runtests.py

Development Installation

Clone the repository:

git clone https://github.com/sajjad-ch/django-dynamic-query.git

Install dependencies:

pip install -r requirements.txt

Run tests:

pytest

Contributing

Contributions are welcome.

If you would like to improve the package:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Open a Pull Request

License

This project is licensed under the MIT License.

See the LICENSE file for details.


Roadmap

Future improvements include:

  • Additional AI providers
  • Better prompt optimization
  • Query explanation
  • Query caching
  • Admin integration
  • Multi-database support

Author

Developed with ❤️ for the Django community.

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_dynamic_query-0.1.3.tar.gz (152.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dynamic_query-0.1.3-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file django_dynamic_query-0.1.3.tar.gz.

File metadata

  • Download URL: django_dynamic_query-0.1.3.tar.gz
  • Upload date:
  • Size: 152.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for django_dynamic_query-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c00a70c9089e137a6f6ab1a115f03e89fb84bf5ece1229b8dda8dba4fd490a88
MD5 77b7f583610c349afe71893c32852765
BLAKE2b-256 5b13a89f823bf78808ee3f0f5f770033a7f10a00cc3c3304985772b7a7ebde0c

See more details on using hashes here.

File details

Details for the file django_dynamic_query-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dynamic_query-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0032d7005d20c2f86486bc39fa60c6864009468c6202f6c87e2d55b49c32c70c
MD5 348eed081afcd2738b6e9f467a1bae55
BLAKE2b-256 16f0e20b2b3f1c68a1054e3ec07a6f16d802b00575d6765c2c2a549f30784a44

See more details on using hashes here.

Supported by

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