Models & Migrations¶
Django Models
Unchained provides seamless integration with Django's ORM for model definition and database management.
Basic Models¶
Common Model Patterns¶
Soft Delete¶
class SoftDeleteModel(BaseModel):
is_deleted = models.BooleanField(default=False)
class Meta:
abstract = True
def delete(self, *args, **kwargs):
self.is_deleted = True
self.save()
class User(SoftDeleteModel):
name = models.CharField(max_length=255)
Timestamps¶
class TimestampModel(BaseModel):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Post(TimestampModel):
title = models.CharField(max_length=255)
Custom Manager¶
class ActiveUserManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active=True)
class User(BaseModel):
name = models.CharField(max_length=255)
is_active = models.BooleanField(default=True)
objects = models.Manager()
active = ActiveUserManager()
Database Migrations¶
Unchained uses Django's migration system to manage database schema changes. After defining or modifying your models, you'll need to create and apply migrations.
Creating Migrations¶
To create new migrations based on model changes:
Where [name]
is an optional name for the migration (e.g., "add_user_model").
Applying Migrations¶
To apply migrations and update your database schema:
Viewing Migration Status¶
To see which migrations have been applied:
For detailed information about migration commands and options, see the CLI documentation.
Best Practices¶
- Use abstract models for common fields and behavior
- Add Meta options for ordering, indexes, and constraints
- Create migrations early to catch model issues
- Keep migrations small to make deployments safer
- Test migrations on a copy of production data when possible
For information about generating CRUD endpoints from your models, see the CRUD documentation.