No project description provided
Project description
🧾 django-annotate
Annotate Django model files with schema information (fields, indexes, and foreign keys), inspired by the annotaterb project for Rails.
✨ Features
- Adds
# == Schema Informationblocks above each model class - Supports:
- Field types
- Indexes (including unique constraints)
- Foreign key relationships
- Works on monolithic or multi-file model setups
- CLI support via Django's
manage.py
🗄️ Database Support
Currently, django-annotate only supports PostgreSQL databases. This is because it uses PostgreSQL-specific system tables (pg_*) to introspect the database schema. Support for other databases (MySQL, SQLite) is planned for future releases.
Requirements
- PostgreSQL database
- Django project configured to use PostgreSQL as the database backend
- Appropriate database permissions to query system tables
If you're using a different database backend, you'll need to either:
- Switch to PostgreSQL
- Wait for support for your database to be added
- Contribute support for your database backend
📦 Installation
pip install django-annotate
# or if using Poetry:
poetry add django-annotate
⚙️ Setup
No configuration required — just install and run. If you want to run it via Django's CLI, ensure django_annotate is on your Python path (you don't need to add it to INSTALLED_APPS unless you want auto-discovery inside Django).
Then run:
python manage.py annotate_models
To annotate a specific app:
python manage.py annotate_models --app=myapp
🔄 Auto-Annotation After Migrations
By default, model annotation runs automatically after every migration. This helps keep your model files up-to-date with your database schema.
To skip auto-annotation during a migration, set the environment variable:
DJANGO_ANNOTATE_SKIP_ON_MIGRATE=1 python manage.py migrate
🧪 Example Output
# == Schema Information
#
# Table name: organizations
#
# id :bigint not null, primary key
# name :varchar(255) not null
# created_at :timestamp not null
#
# Indexes
#
# organizations_pkey (id)
#
# Foreign Keys
#
# (none)
class Organization(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :varchar(254) not null
# full_name :varchar(255) not null
# organization_id :bigint not null
# created_at :timestamp not null
#
# Indexes
#
# users_email_key (email) UNIQUE
# users_organization_id_idx (organization_id)
#
# Foreign Keys
#
# organization_id => organizations.id
class User(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
email = models.EmailField(unique=True)
full_name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
# == Schema Information
#
# Table name: profiles
#
# id :bigint not null, primary key
# user_id :bigint not null
# bio :text
#
# Indexes
#
# profiles_user_id_key (user_id) UNIQUE
#
# Foreign Keys
#
# user_id => users.id
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(blank=True)
# == Schema Information
#
# Table name: projects
#
# id :bigint not null, primary key
# name :varchar(255) not null
# created_at :timestamp not null
#
# Indexes
#
# projects_pkey (id)
#
# Foreign Keys
#
# (none)
class Project(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
members = models.ManyToManyField(User, related_name="projects")
📜 License
MIT © 2025 Chris Davis
⚙️ Configuration
You can configure django-annotate by creating a .django_annotate.yml file in your project root. Here's an example configuration:
# Skip annotation during migrations
skip_on_migrate: false
# Where to place the annotation (before or after the model class)
position: before
# Output format (plain, markdown, or rdoc)
format: plain
# What to include in the annotation
show_indexes: true
show_foreign_keys: true
show_comments: false
# Models and apps to ignore
ignore_models:
- "User"
- "Group"
ignore_apps:
- "auth"
- "admin"
Environment variables override the configuration file. For example:
DJANGO_ANNOTATE_SKIP_ON_MIGRATE=1 python manage.py migrate
⚠️ Production Safety
By default, django-annotate will not run in production environments. This is enforced by:
- Not running when
DEBUG = False(typical in production) - Respecting
DJANGO_ANNOTATE_DISABLE_AUTO = Truesetting
To ensure safety in production, add this to your production settings:
DJANGO_ANNOTATE_DISABLE_AUTO = True
The recommended workflow is:
- Run migrations and annotations locally
- Commit the annotated files to version control
- Deploy the committed files to production
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_annotate-0.2.0.tar.gz.
File metadata
- Download URL: django_annotate-0.2.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
535a43e2f50b4f08091c84e3c7d3093fe34ff10ccbdd27345d822c7a1c93e77f
|
|
| MD5 |
e8bfc28bc0ad030e275f937fae1c4807
|
|
| BLAKE2b-256 |
5d46b7a603865f96f14c030aef0ff2c0636284cd1883b46c3317522bc8e01afb
|
File details
Details for the file django_annotate-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_annotate-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
640bade08400ab8a52f27848aaea6e1dac33f66199262bdc518b8f47d6124f2d
|
|
| MD5 |
c97baf77642f7d79b4055eb9df3ecf20
|
|
| BLAKE2b-256 |
3313c7b4e00e646b52d7a2c182f0035f31942648cb87af046b17f2ffe94a43e2
|