Skip to main content

Query your Django database in natural language from the terminal or Django Admin.

Project description

django-ai-command

Query your Django database in natural language — from the terminal or directly inside the Django Admin.

Powered by LangChain, compatible with Gemini, Groq, DeepSeek, OpenAI, and Anthropic.


How it works

django-ai-command introspects your Django models automatically, sends the schema + your question to an LLM, generates safe read-only ORM code, executes it, and returns the result.

No manual tool configuration. No hardcoded models. Just install, configure your API key, and ask.


Features

  • Natural language queries from the terminal
  • Floating chat widget embedded in the Django Admin (available on every admin page)
  • Full-page chat view at /your-admin-prefix/ai-assistant/
  • Multi-provider support via LangChain (Groq, Gemini, DeepSeek, OpenAI, Anthropic)
  • Safe mode: blocks all write operations by default
  • Automatic model introspection — no setup required per model
  • Designed to become a standalone PyPI package

Screenshots

Terminal command

Terminal command

Floating modal in Django Admin

Floating modal

Full-page chat view

Full page


Installation

For now, copy the django_ai_command/ folder into your project root.

Install dependencies:

pip install langchain langchain-core langchain-openai langchain-google-genai

Add to INSTALLED_APPSmust be before django.contrib.admin:

INSTALLED_APPS = ['django_ai_command'] + CORE_APPS + EXTERNAL_APPS + LOCAL_APPS

Configuration

Add DJANGO_AI_COMMAND to your settings.py:

DJANGO_AI_COMMAND = {
    "provider": "groq",             # LLM provider (see options below)
    "api_key": env("GROQ_API_KEY"), # API key for the selected provider
    "safe_mode": True,              # Block write operations (recommended)
    "admin_prefix": "admin",        # Your admin URL prefix (see note below)
}

admin_prefix

If you changed the default Django admin URL for security reasons (recommended), set admin_prefix to match. For example, if your admin is at /15_admin_27/, set:

"admin_prefix": "15_admin_27",

This ensures the chat widget inside the Admin points to the correct endpoint. If you use the default /admin/ URL, you can omit this field.


Provider examples

Groq (free, recommended for development)

Get your API key at console.groq.com.

DJANGO_AI_COMMAND = {
    "provider": "groq",
    "api_key": env("GROQ_API_KEY"),
    "safe_mode": True,
    "admin_prefix": "admin",
}

Gemini (Google)

Get your API key at aistudio.google.com.

DJANGO_AI_COMMAND = {
    "provider": "gemini",
    "api_key": env("GEMINI_API_KEY"),
    "safe_mode": True,
    "admin_prefix": "admin",
}

DeepSeek

Get your API key at platform.deepseek.com.

DJANGO_AI_COMMAND = {
    "provider": "deepseek",
    "api_key": env("DEEPSEEK_API_KEY"),
    "base_url": "https://api.deepseek.com",
    "safe_mode": True,
    "admin_prefix": "admin",
}

OpenAI

DJANGO_AI_COMMAND = {
    "provider": "openai",
    "api_key": env("OPENAI_API_KEY"),
    "safe_mode": True,
    "admin_prefix": "admin",
}

Anthropic (Claude)

DJANGO_AI_COMMAND = {
    "provider": "anthropic",
    "api_key": env("ANTHROPIC_API_KEY"),
    "safe_mode": True,
    "admin_prefix": "admin",
}

Usage

Terminal

python manage.py ai "How many users registered this week?"
python manage.py ai "List the 5 products with the most stock"
python manage.py ai "Which orders have been pending for more than 3 days?"
python manage.py ai "¿Cuántos usuarios hay registrados?"

Django Admin — floating button

A floating 🤖 button appears on every page of the Django Admin. Click it to open the chat modal and ask questions without leaving your current page.

Django Admin — full page

Navigate to:

/your-admin-prefix/ai-assistant/

Example: https://yourdomain.com/admin/ai-assistant/


Safe mode

When safe_mode is True (default), the following ORM operations are blocked:

delete, update, save, create, bulk_create, bulk_update, raw, extra, execute

The LLM is also instructed via prompt to only generate read operations. Safe mode provides two layers of protection: prompt-level and AST-level validation before execution.


Project structure

django_ai_command/
├── admin.py            # Injects routes and context into Django Admin
├── apps.py             # AppConfig — loads admin.py via ready()
├── executor.py         # Executes ORM code safely (AST validation)
├── introspection.py    # Reads all registered Django models automatically
├── llm.py              # LangChain wrapper — multi-provider support
├── urls.py             # URL definitions for chat views
├── utils.py
├── views.py            # AiChatView (full page) and AiAskView (POST endpoint)
├── management/
│   └── commands/
│       └── ai.py       # Management command: python manage.py ai "question"
├── templates/
│   ├── admin/
│   │   └── base_site.html          # Injects floating button into all admin pages
│   └── django_ai_command/
│       └── ai_chat.html            # Full-page chat view template
└── docs/
    ├── img_01.png      # Terminal command screenshot
    ├── img_02.png      # Floating modal screenshot
    └── img_03.png      # Full-page view screenshot

Roadmap

Version Description
v1.0 Terminal command + Gemini
v2.0 Terminal command + multi-provider via LangChain ✅
v3.0 Chat widget in Django Admin ✅
v4.0 LLM-suggested charts rendered in the Admin

License

MIT


Author

Sebastián Martín Artaza Saade

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_ai_command-1.0.1.tar.gz (604.2 kB view details)

Uploaded Source

Built Distribution

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

django_ai_command-1.0.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file django_ai_command-1.0.1.tar.gz.

File metadata

  • Download URL: django_ai_command-1.0.1.tar.gz
  • Upload date:
  • Size: 604.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for django_ai_command-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3bbc29fec0ef87251e5800fbeb2ed14eef24b768d5a16a869c5c1fcb877b0f4c
MD5 c833cb29fcf167aa752f946714ad01d7
BLAKE2b-256 c1c77bd9862fac5af426008f0dda08658d350b851e282e9fd4a941e8981de9d5

See more details on using hashes here.

File details

Details for the file django_ai_command-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ai_command-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b0bffb151b27bce6c85e41246fd4751e20cbf26607a0b61d5b2e31a198757cc0
MD5 b756452e7fc5003b693bfb149d1e1ee2
BLAKE2b-256 8d9719c38846a59fc57bdeff9c621564adea1dcf5b1d6a3df7f89632e0237b3f

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