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.4.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.4-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_dynamic_query-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 d92ed1e27719ce7b4cf4251fdc92936c5c6b3949f31d654b4dcea2bb0efb774e
MD5 eeabbb772b36631e84380c234f0763e1
BLAKE2b-256 5b61fb0cdd048714627cbe6a182dd4c2c17e2c94f11b3dddd9c8eca88b788aca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_dynamic_query-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f1f54594ab0fd4d0bba8a814c14267379a73361404ee05d2a033ab713185f469
MD5 2195547eb4323895c0b6ae31a877d7ff
BLAKE2b-256 22fec3f00aff45442d645c2c0b6a0fff5d1d991e86b6707a9e792de1580d6906

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