home / skills / dexploarer / claudius-skills / django-model-helper

This skill generates Django models with proper fields, relationships, and migrations, speeding up schema design and ensuring best practices.

npx playbooks add skill dexploarer/claudius-skills --skill django-model-helper

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
2.0 KB
---
name: django-model-helper
description: Generates Django models with proper field types, relationships, and migrations. Use when creating Django models or database schemas.
allowed-tools: [Write, Read, Bash]
---

# Django Model Helper

Generates Django models following best practices.

## When to Use

- "Create a Django model for users"
- "Generate Product model"
- "Add BlogPost model with relationships"

## Model Generation

```python
from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    """Custom user model."""
    bio = models.TextField(blank=True)
    avatar = models.ImageField(upload_to='avatars/', blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'users'
        ordering = ['-created_at']

    def __str__(self):
        return self.username

class Post(models.Model):
    """Blog post model."""
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    content = models.TextField()
    published_at = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'posts'
        ordering = ['-published_at']
        indexes = [
            models.Index(fields=['slug']),
            models.Index(fields=['author', '-published_at']),
        ]

    def __str__(self):
        return self.title
```

## After Creating Model

1. Generate migration:
   ```bash
   python manage.py makemigrations
   ```

2. Apply migration:
   ```bash
   python manage.py migrate
   ```

## Best Practices

- Use appropriate field types
- Add indexes for frequently queried fields
- Define __str__ methods
- Use Meta class for table name and ordering
- Add related_name to relationships
- Include created_at/updated_at timestamps
- Use on_delete properly
- Add helpful docstrings

Overview

This skill generates Django models, complete with appropriate field types, relationships, Meta options, and migration guidance. It helps translate data requirements into Django ORM classes that follow common conventions and best practices. Use it to speed up schema design and produce ready-to-migrate model code.

How this skill works

Provide entity descriptions, field lists, and relationship requirements; the skill outputs Django model classes with proper fields, related_name, on_delete behavior, indexes, and __str__ methods. It also suggests Meta settings (db_table, ordering) and explicit timestamps, and provides the exact manage.py commands to create and apply migrations. The output is concise, copy-paste ready Python code that integrates with typical Django projects.

When to use it

  • Starting a new Django app and defining initial database models
  • Adding a new entity such as Product, BlogPost, or Order with relationships
  • Refactoring existing models to add indexes, timestamps, or cleaner relations
  • Generating models for APIs or admin interfaces to speed development
  • Preparing migration-ready model definitions for database schema changes

Best practices

  • Choose field types that match data shape (CharField for short text, TextField for long content, DateTimeField for timestamps)
  • Add created_at and updated_at timestamps using auto_now_add and auto_now
  • Specify related_name on ForeignKey/ManyToMany to avoid reverse accessor clashes
  • Add indexes for frequently queried fields and composite indexes for common filters
  • Define __str__ for readable admin displays and add docstrings for models
  • Use Meta to set db_table, ordering, and explicit indexes for performance

Example use cases

  • Generate a User model that extends AbstractUser with bio, avatar, and timestamps
  • Create a BlogPost model with title, slug (unique), content, author foreign key, and indexes
  • Build an e-commerce Product and Category models with many-to-many relationships and search-optimized indexes
  • Refactor models to add ordering, db_table names, and explicit composite indexes before migrations
  • Produce migration commands and code snippets to include in a Django app scaffold

FAQ

Will the skill create migrations automatically?

No. It generates model code and shows the exact commands (python manage.py makemigrations and migrate) you should run to create and apply migrations.

Can it handle custom user models?

Yes. It can generate custom user models that extend AbstractUser or AbstractBaseUser and include additional fields and Meta configuration.