django-best-practices
Skill4 starsChanged 14 months agoBSD-3-ClauseView on GitHub
This skill should be used when working with Django models, admin interfaces, tests, or migrations in this multi-tenant Django project. It provides templates, patterns, and best practices for creating tenant-aware models with proper isolation, admin interfaces with optimized queries, and comprehensive tests. Use when creating new Django models, admin configurations, writing tests, or when you need to follow the multi-tenant architecture patterns used in this codebase.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- baekenough/oh-my-customcodetheir own version, first seen Oct 3, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- baekenough/oh-my-customcode★ 43same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository3 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- django-securitySkill
- django-typingSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
18 rules. About 0 are common, 13 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈13
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Create Django models, admin interfaces, and tests that follow the multi-tenant architecture patterns established in this codebase.
- Distinctive: line 16
Use this skill when:
- Distinctive: line 38
Use the generation script to create boilerplate quickly:
- Very specific: line 56
All data in this project is scoped to a Tenant (organization).
- Distinctive: line 81
Add business logic methods
- Very specific: line 93
Use raw_id_fields or autocomplete_fields for ForeignKeys
- Distinctive: line 94
Add custom actions for bulk operations
- Distinctive: line 459
Consider tenant-first indexes for new fields
- Distinctive: line 464
Remove field entirely in next release
- Distinctive: line 467
Add new field → data migration → remove old field → rename new field
- Very specific: line 488
Always use apps.get_model() (never import models directly)
- Distinctive: line 519
Add nullable tenant FK
- Distinctive: line 522
Add tenant-first indexes and constraints
- Very specific: line 545
Checklist: Use assets/schema_migration_checklist.md for common scenarios:
- Very specific: line 676
To access references: Read the files when you need detailed information about specific patterns.
- Distinctive: line 764
If you encounter issues:
- Distinctive: line 766
Check the reference documentation for detailed patterns
- Distinctive: line 769
Ensure you're following the critical requirements checklist
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Django Best Practices
Guide for creating Django models, admin interfaces, and tests following the multi-tenant architecture and best practices used in this project.
Overview
Create Django models, admin interfaces, and tests that follow the multi-tenant architecture patterns established in this codebase. All models should inherit from TenantBaseModel or TenantAwareModel to ensure proper data isolation, and all code should follow Django best practices for performance, security, and maintainability.
When to Use This Skill
Use this skill when:
- Creating a new Django model
- Building an admin interface for a model
- Writing tests for models or views
- Working with multi-tenant data isolation
- Optimizing Django queries
- Creating migrations for multi-tenant models
- Needing to understand the multi-tenant patterns in this project
Quick Start
Creating a New Model
- Use the model template from
assets/model_template.pyor generate boilerplate with the script - Inherit from
TenantAwareModelfor automatic tenant support - Create proper indexes (tenant-first for all composite indexes!)
- Add validation in
clean()method for cross-field validation - Create admin interface using
assets/admin_template.py - Write tests using
assets/test_template.py
Quick Generation Script
Use the generation script to create boilerplate quickly:
python scripts/generate_model.py notifications Notification \
--description "Represents a user notification" \
--with-admin \
--with-testsThis generates:
- Model code with multi-tenant support
- Admin interface with optimized queries
- Comprehensive test suite
Core Concepts
Multi-Tenant Architecture
All data in this project is scoped to a Tenant (organization). The architecture enforces isolation at multiple layers:
Base Models:
TenantBaseModel- ProvidestenantFK and automatic filteringTenantAwareModel- Adds UUIDid,created_at,updated_attimestamps
Key Patterns:
- Tenant context set by middleware from
request.user.profile.tenant - QuerySets automatically filtered by current tenant via
TenantManager - Indexes always put
tenantfirst in composite indexes - Unique constraints include
tenantfield - Validation ensures cross-tenant references don't occur
Read the detailed guide: references/multi-tenant-patterns.md
Model Best Practices
Standard Model Structure:
- Inherit from
TenantAwareModel - Define fields with
help_text - Set up relationships with explicit
related_name - Configure Meta with indexes (tenant-first!), constraints, ordering
- Implement
__str__and__repr__ - Override
save()for auto-population logic - Implement
clean()for validation - Add business logic methods
Read the detailed guide: references/model-patterns.md
Admin Interface Best Practices
Standard Admin Configuration:
- Use
@admin.register()decorator - Configure
list_display,list_filter,search_fields - Optimize with
list_select_relatedorget_queryset() - Organize form with
fieldsets - Mark metadata as
readonly_fields - Use
raw_id_fieldsorautocomplete_fieldsfor ForeignKeys - Add custom actions for bulk operations
- Filter by tenant for non-superusers
Read the detailed guide: references/admin-patterns.md
Creating a New Model
Step 1: Choose Your Base Class
from apps.core.models.base import TenantAwareModel # Recommended
class MyModel(TenantAwareModel):
"""
Your model description.
Inherits:
- id (UUIDField)
- tenant (ForeignKey)
- created_at, updated_at (DateTimeField)
"""
passStep 2: Define Fields
class MyModel(TenantAwareModel):
# Core fields
name = models.CharField(max_length=200, help_text='...')
slug = models.SlugField(max_length=200, help_text='...')
# Relationships
user = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
related_name='mymodel_set'
)
workspace = models.ForeignKey(
'workspace.Workspace',
on_delete=models.CASCADE,
related_name='mymodels'
)
# Status
is_active = models.BooleanField(default=True)Step 3: Configure Meta
class Meta:
db_table = 'app_mymodel'
verbose_name = 'My Model'
ordering = ['-created_at']
# Indexes - TENANT FIRST!
indexes = [
models.Index(fields=['tenant', 'user', '-created_at']),
models.Index(fields=['tenant', 'workspace', '-created_at']),
]
# Constraints - include tenant
constraints = [
models.UniqueConstraint(
fields=['tenant', 'workspace', 'slug'],
name='unique_mymodel_slug'
),
]Step 4: Add Methods
def __str__(self):
return f"{self.name} ({self.workspace.name})"
def save(self, *args, **kwargs):
"""Auto-populate fields."""
if not self.slug:
from django.utils.text import slugify
self.slug = slugify(self.name)
if not self.tenant_id and self.workspace:
self.tenant = self.workspace.tenant
super().save(*args, **kwargs)
def clean(self):
"""Validate data."""
super().clean()
# Validate tenant consistency
if self.workspace and self.tenant:
if self.workspace.tenant != self.tenant:
raise ValidationError({
'workspace': 'Must belong to same tenant'
})Creating an Admin Interface
Step 1: Basic Registration
from django.contrib import admin
from .models import MyModel
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
"""Admin interface for MyModel."""
passStep 2: Configure List View
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ['name', 'user', 'workspace', 'is_active', 'created_at']
list_filter = ['is_active', 'created_at']
search_fields = ['name', 'user__username', 'workspace__name']
list_select_related = ['user', 'workspace', 'tenant']
date_hierarchy = 'created_at'Step 3: Configure Form
readonly_fields = ['id', 'created_at', 'updated_at']
raw_id_fields = ['user']
autocomplete_fields = ['workspace']
fieldsets = (
('Basic Information', {
'fields': ('name', 'slug')
}),
('Relationships', {
'fields': ('user', 'workspace')
}),
('Metadata', {
'fields': ('id', 'created_at', 'updated_at'),
'classes': ('collapse',)
}),
)Step 4: Optimize Queries
def get_queryset(self, request):
"""Optimize and filter by tenant."""
qs = super().get_queryset(request)
qs = qs.select_related('user', 'workspace', 'tenant')
# Filter by tenant for non-superusers
if not request.user.is_superuser and hasattr(request.user, 'profile'):
qs = qs.filter(tenant=request.user.profile.tenant)
return qsStep 5: Add Actions
actions = ['activate_selected', 'deactivate_selected']
def activate_selected(self, request, queryset):
"""Activate selected records."""
updated = queryset.update(is_active=True)
self.message_user(request, f"Activated {updated} records")
activate_selected.short_description = "Activate selected items"Writing Tests
Step 1: Set Up Test Case
from django.test import TestCase
from django.contrib.auth import get_user_model
from apps.users.models import Tenant, UserProfile
from apps.users.middleware import set_current_tenant
from .models import MyModel
User = get_user_model()
class MyModelTest(TestCase):
def setUp(self):
# Create tenant
self.tenant = Tenant.objects.create(name="Test Org", slug="test")
set_current_tenant(self.tenant)
# Create user with profile
self.user = User.objects.create_user(username='test', password='pass')
self.user.profile = UserProfile.objects.create(
user=self.user,
tenant=self.tenant
)
def tearDown(self):
set_current_tenant(None)Step 2: Test Basic Operations
def test_create_model(self):
"""Test creating instance."""
obj = MyModel.objects.create(
name="Test",
user=self.user
)
self.assertEqual(obj.name, "Test")
self.assertEqual(obj.tenant, self.tenant)
def test_str_representation(self):
"""Test __str__ method."""
obj = MyModel.objects.create(name="Test", user=self.user)
self.assertEqual(str(obj), "Test")Step 3: Test Tenant Isolation
def test_tenant_isolation(self):
"""Test multi-tenant isolation."""
# Create object for tenant1
obj1 = MyModel.objects.create(name="T1", user=self.user)
# Create tenant2
tenant2 = Tenant.objects.create(name="Org2", slug="org2")
user2 = User.objects.create_user(username='user2', password='pass')
user2.profile = UserProfile.objects.create(user=user2, tenant=tenant2)
# Switch to tenant2
set_current_tenant(tenant2)
obj2 = MyModel.objects.create(name="T2", user=user2)
# Verify isolation
self.assertEqual(MyModel.objects.count(), 1)
self.assertEqual(MyModel.objects.first(), obj2)
# Switch back
set_current_tenant(self.tenant)
self.assertEqual(MyModel.objects.count(), 1)
self.assertEqual(MyModel.objects.first(), obj1)
# Unfiltered sees both
set_current_tenant(None)
self.assertEqual(MyModel.objects.unfiltered().count(), 2)Step 4: Test Validation
def test_cross_tenant_validation(self):
"""Test cross-tenant reference prevention."""
tenant2 = Tenant.objects.create(name="Org2", slug="org2")
# ... create user2, workspace2 for tenant2
obj = MyModel(
name="Test",
user=self.user, # tenant1
workspace=workspace2, # tenant2
tenant=self.tenant
)
with self.assertRaises(ValidationError):
obj.clean()Common Patterns
Pattern: Soft Delete
class SoftDeleteModel(TenantAwareModel):
is_deleted = models.BooleanField(default=False, db_index=True)
deleted_at = models.DateTimeField(null=True, blank=True)
def delete(self, *args, **kwargs):
"""Soft delete."""
self.is_deleted = True
self.deleted_at = timezone.now()
self.save(update_fields=['is_deleted', 'deleted_at', 'updated_at'])Pattern: Audit Trail
class AuditedModel(TenantAwareModel):
created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='+')
modified_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='+')
def save(self, *args, **kwargs):
user = kwargs.pop('user', None)
if user:
if not self.pk:
self.created_by = user
self.modified_by = user
super().save(*args, **kwargs)Pattern: Custom Manager/QuerySet
class MyModelQuerySet(models.QuerySet):
def active(self):
return self.filter(is_active=True)
def for_user(self, user):
return self.filter(user=user)
class MyModelManager(models.Manager):
def get_queryset(self):
return MyModelQuerySet(self.model, using=self._db)
def active(self):
return self.get_queryset().active()
class MyModel(TenantAwareModel):
# ... fields ...
objects = MyModelManager()Creating and Managing Migrations
Quick Start with Migrations
Basic workflow:
# 1. Make model changes in models.py
# 2. Generate migration
python manage.py makemigrations app_name
# 3. Review generated migration file
# 4. Test migration
python manage.py migrate
# 5. Test reversibility
python manage.py migrate app_name previous_migration_name
python manage.py migrate # Re-applyUse the migration helper for validation:
# Check status and validate
python scripts/migration_helper.py status
# Create migration with validation
python scripts/migration_helper.py create app_name
# Validate existing migrations
python scripts/migration_helper.py validateSchema Migrations
Adding fields:
- Provide default value or make nullable
- Consider tenant-first indexes for new fields
- Review auto-generated migration before applying
Removing fields (two-step):
- Make field nullable, deploy
- Remove field entirely in next release
Changing field types:
- Add new field → data migration → remove old field → rename new field
Adding indexes:
class Meta:
indexes = [
# Always put tenant first!
models.Index(fields=['tenant', 'status', '-created_at']),
]Data Migrations
Create empty migration:
python manage.py makemigrations --empty app_name --name populate_fieldUse the template: Copy assets/data_migration_template.py for comprehensive examples.
Key principles:
- Always use
apps.get_model()(never import models directly) - Batch large updates to avoid memory issues
- Provide reverse function for reversibility
- Handle null/missing data gracefully
Example:
def forward(apps, schema_editor):
MyModel = apps.get_model('myapp', 'MyModel')
for obj in MyModel.objects.iterator(chunk_size=1000):
obj.new_field = obj.old_field.upper()
obj.save(update_fields=['new_field'])
def reverse(apps, schema_editor):
MyModel = apps.get_model('myapp', 'MyModel')
for obj in MyModel.objects.iterator(chunk_size=1000):
obj.old_field = obj.new_field.lower()
obj.save(update_fields=['old_field'])
class Migration(migrations.Migration):
operations = [
migrations.RunPython(forward, reverse),
]Multi-Tenant Migration Patterns
Adding tenant support to existing models (4-step process):
- Add nullable tenant FK
- Data migration to assign default tenant
- Make tenant required
- Add tenant-first indexes and constraints
See references/migration-patterns.md for complete examples.
Migration Best Practices
Always do:
- ✅ Review generated migrations before committing
- ✅ Test reversibility (migrate backward then forward)
- ✅ Use apps.get_model() in data migrations
- ✅ Put tenant first in all composite indexes
- ✅ Batch large data updates
- ✅ Test on production-like data
Never do:
- ❌ Edit applied migrations (create new one instead)
- ❌ Import models directly in migrations
- ❌ Skip testing reversibility
- ❌ Forget tenant in indexes/constraints
- ❌ Use --fake without understanding implications
Migration Resources
Checklist: Use assets/schema_migration_checklist.md for common scenarios:
- Adding/removing fields
- Changing field types
- Adding indexes and constraints
- Data migrations
- Multi-tenant migrations
Reference: See references/migration-patterns.md for:
- Detailed migration patterns
- Complex scenarios
- Troubleshooting guide
- Advanced techniques
Helper script: scripts/migration_helper.py validates migrations and checks for:
- Tenant-first indexes
- Unique constraints with tenant
- Direct model imports
- Missing reverse functions
- Common pitfalls
Critical Requirements
Always follow these rules:
- ✅ Inherit from
TenantAwareModelfor tenant-scoped models - ✅ Put
tenantfirst in all composite indexes - ✅ Include
tenantin all unique constraints - ✅ Validate tenant consistency in
clean()method - ✅ Use
help_texton all fields for documentation - ✅ Specify
related_nameon all ForeignKeys - ✅ Optimize admin queries with
select_related/prefetch_related - ✅ Test tenant isolation in all test suites
- ✅ Use
readonly_fieldsfor metadata in admin - ✅ Filter by tenant for non-superusers in admin
Never do these:
- ❌ Don't use
all_objectsmanager in views (admin only!) - ❌ Don't create cross-tenant references without validation
- ❌ Don't forget tenant field in indexes and constraints
- ❌ Don't manually add
tenantfield (use TenantBaseModel) - ❌ Don't skip
clean()validation - ❌ Don't use N+1 queries in admin (optimize with select_related)
Resources
scripts/
generate_model.py - Generate Django model boilerplate with multi-tenant support
Usage:
python scripts/generate_model.py <app_name> <model_name> [--with-admin] [--with-tests]Example:
python scripts/generate_model.py notifications Notification \
--description "User notification" \
--with-admin \
--with-testsmigration_helper.py - Django migration helper with validation and best practice checks
Usage:
# Check migration status
python scripts/migration_helper.py status
# Create migration with validation
python scripts/migration_helper.py create app_name
# Validate existing migrations
python scripts/migration_helper.py validate
# Check for conflicts
python scripts/migration_helper.py check-conflicts
# Show SQL for migration
python scripts/migration_helper.py sql app_name migration_numberassets/
Copy-paste templates for quick development:
model_template.py- Complete model template with all best practicesadmin_template.py- Admin interface template with optimizationtest_template.py- Comprehensive test suite templatedata_migration_template.py- Data migration template with RunPython examplesschema_migration_checklist.md- Checklist for common migration scenarios
Copy these templates and customize for your specific model.
references/
Detailed documentation for deep understanding:
multi-tenant-patterns.md- Complete guide to multi-tenant architecture- TenantBaseModel and TenantAwareModel
- TenantQuerySet and TenantManager
- Middleware integration
- Query patterns
- Testing multi-tenancy
- Multi-tenant migration patterns
model-patterns.md- Django model best practices- Model structure and organization
- Field patterns and choices
- Meta options (indexes, constraints, ordering)
- Validation patterns
migration-patterns.md- Django migration comprehensive guide- Schema migrations (adding/removing/altering fields, indexes, constraints)
- Data migrations (RunPython, RunSQL patterns)
- Multi-tenant migration patterns
- Migration dependencies and ordering
- Reversible migrations
- Testing migrations
- Squashing migrations
- Troubleshooting (conflicts, rollbacks, debugging)
admin-patterns.md- Django admin best practices- List display and filters
- Custom display methods
- QuerySet optimization
- Permissions and tenant filtering
- Custom actions
- Inline admin
To access references: Read the files when you need detailed information about specific patterns. The SKILL.md provides overview and quick patterns; references provide comprehensive details.
Examples
Example 1: Create a Notification Model
# apps/notifications/models.py
from apps.core.models.base import TenantAwareModel
from django.db import models
class Notification(TenantAwareModel):
"""User notification."""
user = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
related_name='notifications'
)
title = models.CharField(max_length=200)
message = models.TextField()
is_read = models.BooleanField(default=False)
link = models.URLField(blank=True)
class Meta:
db_table = 'notifications_notification'
ordering = ['-created_at']
indexes = [
models.Index(fields=['tenant', 'user', 'is_read', '-created_at']),
]Example 2: Create Admin with Bulk Actions
# apps/notifications/admin.py
@admin.register(Notification)
class NotificationAdmin(admin.ModelAdmin):
list_display = ['title', 'user', 'is_read', 'created_at']
list_filter = ['is_read', 'created_at']
search_fields = ['title', 'message', 'user__username']
readonly_fields = ['id', 'created_at', 'updated_at']
raw_id_fields = ['user']
actions = ['mark_as_read', 'mark_as_unread']
def mark_as_read(self, request, queryset):
queryset.update(is_read=True)
self.message_user(request, f"Marked {queryset.count()} as read")
mark_as_read.short_description = "Mark selected as read"
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.select_related('user', 'tenant')Example 3: Test Tenant Isolation
# apps/notifications/tests.py
class NotificationTest(TestCase):
def setUp(self):
self.tenant = Tenant.objects.create(name="Org", slug="org")
set_current_tenant(self.tenant)
self.user = User.objects.create_user(username='user', password='pass')
self.user.profile = UserProfile.objects.create(
user=self.user,
tenant=self.tenant
)
def test_isolation(self):
n1 = Notification.objects.create(
user=self.user,
title="Test",
message="Message"
)
self.assertEqual(n1.tenant, self.tenant)
# Create tenant2
tenant2 = Tenant.objects.create(name="Org2", slug="org2")
set_current_tenant(tenant2)
# Shouldn't see n1
self.assertEqual(Notification.objects.count(), 0)Getting Help
If you encounter issues:
- Check the reference documentation for detailed patterns
- Review the templates in
assets/for complete examples - Use the generation script to create boilerplate quickly
- Ensure you're following the critical requirements checklist
The multi-tenant architecture is the foundation of this project. When in doubt, prioritize tenant isolation and data security.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

