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.
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_dynamic_query-0.1.0.tar.gz.
File metadata
- Download URL: django_dynamic_query-0.1.0.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
449b6855e437594ec59c0d37d0d2f5c814e053a3112040c42ed463a2f0d0b66a
|
|
| MD5 |
e9921479a8d5755b42c7037bbc57e88a
|
|
| BLAKE2b-256 |
7da1ff574bf25dcdcbbb9b37c88cae84593231e467caf209680a87f1e8829207
|
File details
Details for the file django_dynamic_query-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_dynamic_query-0.1.0-py3-none-any.whl
- Upload date:
- Size: 24.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bd6a1cbfbefae591699814f5f48caf4b781c635a87ab71eade2fd396eb526b7
|
|
| MD5 |
960f61642afa25361275992c3f1f7f6e
|
|
| BLAKE2b-256 |
ec72888fd68dc267b7f697928200ed95d918eddc0d5d2aa51619c3544b1d6ab5
|