models

Django model signals vs overriding save

Signals and save overrides both handle model events, but have different use cases. I override save() for logic intrinsic to the model. Signals decouple logic across apps—I use them when multiple apps need to respond to model changes. Signals can make

Django model save override for custom logic

Overriding the save() method allows custom behavior before or after saving. I call super().save() to preserve default behavior. Common use cases include auto-generating slugs, updating related objects, or validating business rules. I use created check

Django model managers for custom querysets

Custom managers encapsulate common query patterns. I create a manager method for each frequently-used filter like published() or active(). By returning self, I can chain manager methods. For more complex queries, I create a custom QuerySet class and u

Django custom user model best practices

Extending Django's user model should be done early in projects. I use AbstractBaseUser for full control or AbstractUser to extend the default. Setting AUTH_USER_MODEL points Django to my custom model. I add fields like phone, avatar, or preferences. F

Django custom user model with email authentication

Using email instead of username for authentication is a common requirement. I extend AbstractBaseUser and create a custom user manager early in the project because switching later means complex data migrations. The key is setting USERNAME_FIELD = 'ema

Django model validation with clean method

The clean() method validates model instances before saving. I raise ValidationError for invalid data. This runs on form submission and can be called explicitly with full_clean(). I validate cross-field constraints that can't be expressed as field vali

Django model inheritance with proxy models

Proxy models create different Python behavior for the same database table. I set proxy = True in Meta. This adds methods or changes default ordering without new tables. Proxy models share the same table as the original. Use cases include different man

Django contenttypes framework for generic relations

ContentTypes enable generic foreign keys pointing to any model. I use GenericForeignKey for features like comments, tags, or favorites on multiple content types. The framework tracks all installed models. I query with content_type and object_id. For r

Django model soft delete pattern

Soft deletes mark records as deleted without removing them from the database. I add a deleted_at field and override the delete() method. A custom manager excludes soft-deleted records by default. I provide a hard_delete() method for permanent removal.

Django model meta options for database optimization

Model Meta class controls database behavior. I use ordering for default query ordering, indexes for database indexes, and unique_together for composite uniqueness constraints. The db_table option customizes table names. For large tables, managed=False

Django model property for computed fields

Properties let me add computed attributes to models without storing them in the database. I use @property for simple calculations like full name or age. For expensive computations, I consider caching the result in a field and updating it via signals o

Django model inheritance with abstract base classes

Abstract base classes let me define common fields and methods without creating database tables. I set abstract = True in Meta. Concrete models inheriting from the abstract class get all its fields and methods. This is perfect for timestamps, soft dele