Models¶
This module provides abstract base models and mixins for consistent model design across Django applications using drf-corekit.
BaseModel¶
Provides a consistent primary key field across all models.
PublicIDMixin¶
Adds a public facing UUID identifier.
CreatedAtMixin¶
Adds creation timestamp tracking.
LastModifiedAtMixin¶
Tracks last modification time automatically.
CommonModel¶
A standard base model combining:
BaseModelCreatedAtMixinLastModifiedAtMixin
Use this as the default base for most internal models.
CommonWithPublicIDModel¶
Extends CommonModel with a public UUID identifier.
AbstractUser¶
Abstract base user model providing auth fields, account state, and lifecycle methods. Designed to be subclassed once per project.
Inherits from BaseModel, PublicIDMixin, LastModifiedAtMixin, Django's AbstractBaseUser, and PermissionsMixin.
Usage¶
from drf_corekit.users import AbstractUser
class User(AbstractUser):
user_type = models.IntegerField(
choices=UserType.choices,
default=UserType.USER,
)
class Meta(AbstractUser.Meta):
db_table = to_app_table_name("users")
Set AUTH_USER_MODEL in your Django settings:
Fields¶
| Field | Type | Description |
|---|---|---|
username |
CharField |
Unique login identifier |
email |
EmailField |
Email address; null if not provided |
is_staff |
BooleanField |
Django admin access |
is_active |
BooleanField |
Account can authenticate; false while suspended or scheduled for deletion |
is_suspended |
BooleanField |
Admin-imposed block; also sets is_active=False |
is_scheduled_for_deletion |
BooleanField |
Queued for hard deletion by a management command |
joined_at |
DateTimeField |
Account creation timestamp |
verified_at |
DateTimeField |
Email verification timestamp; null until verified |
suspended_at |
DateTimeField |
When suspension was applied; null if not suspended |
suspension_reason |
CharField |
Admin-supplied reason; null if not suspended |
deletion_scheduled_at |
DateTimeField |
When deletion was scheduled; null if not scheduled |
Account state¶
is_active follows two flags: the account is inactive while it is suspended or scheduled for deletion. Django's login and DRF authentication refuse inactive users, so these methods are what block access. Each method also saves last_modified_at.
| Method | is_active after |
|---|---|
suspend() |
False |
unsuspend() |
True, unless scheduled for deletion |
schedule_deletion() |
False |
cancel_deletion() |
True, unless suspended |
Warning
Do not set is_active by hand for other reasons (for example an unverified email) on an account you also suspend: unsuspend() and cancel_deletion() turn it back on.
Methods¶
suspend(reason=None)¶
Marks the account as suspended, deactivates it, and records the timestamp and optional reason.
unsuspend()¶
Clears suspension state and wipes the recorded reason and timestamp. Reactivates the account unless it is scheduled for deletion.
schedule_deletion()¶
Flags the account for deferred hard deletion and deactivates it. The actual deletion is performed by a management command.
cancel_deletion()¶
Revokes a previously scheduled deletion. Reactivates the account unless it is suspended.
email_user(subject, message, from_email=None, **kwargs)¶
Sends an email to the user via django.core.mail.send_mail.
user.email_user(
subject="Your account has been suspended",
message="Please contact support for more information.",
)
Configuration¶
Username field behaviour is controlled via DRF_COREKIT settings.
See Configurations for details.