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
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:
- Fork the repository
- Create a feature branch
- Commit your changes
- 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.
Metadata
Release files for django-dynamic-query 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_dynamic_query-0.1.4.tar.gz | 152.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_dynamic_query-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 177.1 kB
Release files / django_dynamic_query-0.1.4.tar.gz
| Download URL | django_dynamic_query-0.1.4.tar.gz |
|---|---|
| Size | 152.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d92ed1e27719ce7b4cf4251fdc92936c5c6b3949f31d654b4dcea2bb0efb774e
|
|
BLAKE2b-256 checksum How to use checksums |
5b61fb0cdd048714627cbe6a182dd4c2c17e2c94f11b3dddd9c8eca88b788aca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Release files / django_dynamic_query-0.1.4-py3-none-any.whl
| Download URL | django_dynamic_query-0.1.4-py3-none-any.whl |
|---|---|
| Size | 24.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f1f54594ab0fd4d0bba8a814c14267379a73361404ee05d2a033ab713185f469
|
|
BLAKE2b-256 checksum How to use checksums |
22fec3f00aff45442d645c2c0b6a0fff5d1d991e86b6707a9e792de1580d6906
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|