Skip to main content

EmailAddress

Manages email addresses associated with user accounts, including verification status and primary email designation.

Fields

ForeignKey
Reference to the user model (settings.AUTH_USER_MODEL). On delete: CASCADE.
EmailField
The email address. Indexed for performance. Max length determined by ACCOUNT_EMAIL_MAX_LENGTH setting.
BooleanField
default:"False"
Whether the email address has been verified.
BooleanField
default:"False"
Whether this is the user’s primary email address. Only one email per user can be primary.

Methods

clean()

Normalizes the email address to lowercase before saving.

can_set_verified()

Checks whether the email address can be marked as verified.
bool
Returns True if the email can be verified, False if there’s a conflict with UNIQUE_EMAIL setting.

set_verified(commit=True)

Marks the email address as verified.
bool
default:"True"
Whether to save the change to the database immediately.
bool
Returns True if the email was successfully verified, False otherwise.

set_as_primary(conditional=False)

Marks the email address as the user’s primary email.
bool
default:"False"
If True, only sets as primary if no other primary email exists.
bool
Returns True if successfully set as primary, False if conditional and another primary exists.

send_confirmation(request=None, signup=False)

Creates and sends an email confirmation.
HttpRequest
The HTTP request object.
bool
default:"False"
Whether this confirmation is for signup.
EmailConfirmation
Returns the created confirmation object.

remove()

Deletes the email address and updates the user’s email field if necessary.

Constraints

  • unique_together: (user, email) - Each user can have each email address only once
  • unique_primary_email: Only one primary email per user (enforced via UniqueConstraint)
  • unique_verified_email: If UNIQUE_EMAIL is enabled, verified emails are unique across all users

EmailConfirmation

Represents an email confirmation request with a unique key.

Fields

ForeignKey
Reference to the EmailAddress being confirmed. On delete: CASCADE.
DateTimeField
When the confirmation was created. Defaults to current time.
DateTimeField
When the confirmation email was sent.
CharField
Unique confirmation key (max length 64).

Class Methods

create(email_address)

Creates a new confirmation for an email address.
EmailAddress
The email address to create confirmation for.
EmailConfirmation
Returns the created confirmation object.

from_key(key)

Retrieves a valid confirmation by its key.
str
The confirmation key.
EmailConfirmation | None
Returns the confirmation object if valid, None otherwise.

Instance Methods

key_expired()

Checks if the confirmation key has expired.
bool
Returns True if expired, based on EMAIL_CONFIRMATION_EXPIRE_DAYS setting.

confirm(request)

Confirms the email address if the key hasn’t expired.
HttpRequest
The HTTP request object.
EmailAddress | None
Returns the confirmed EmailAddress, or None if expired.

send(request=None, signup=False)

Sends the confirmation email and updates the sent timestamp.
HttpRequest
The HTTP request object.
bool
default:"False"
Whether this is for signup.

EmailConfirmationHMAC

HMAC-based email confirmation (no database storage). Used when EMAIL_CONFIRMATION_HMAC is enabled.

Class Methods

create(email_address)

Creates an HMAC-based confirmation.
EmailAddress
The email address to confirm.
EmailConfirmationHMAC
Returns the confirmation object.

from_key(key)

Retrieves and validates an HMAC confirmation key.
str
The HMAC-signed key.
EmailConfirmationHMAC | None
Returns the confirmation if valid, None if expired or invalid.

Properties

str
The HMAC-signed confirmation key (read-only property).

Instance Methods

key_expired()

Always returns False as expiration is checked during signature validation.

confirm(request)

Confirms the email address.
HttpRequest
The HTTP request object.
EmailAddress | None
Returns the confirmed EmailAddress.

Login

Represents a user in the process of logging in. Used to track login state across requests.

Attributes

AbstractBaseUser | None
The user being logged in. Optional to prevent user enumeration.
EmailVerificationMethod
Email verification method to use for this login.
str
URL to redirect to after login.
dict
Additional kwargs to pass to signals.
bool
default:"False"
Whether this login is part of signup.
str
Email address used for login.
str
Phone number used for login.
dict
Additional state dictionary.
float
Unix timestamp when login was initiated.

Constructor

Methods

serialize()

Serializes the login state to a dictionary for session storage.
dict
Dictionary containing all login state.

deserialize(data)

Class method to reconstruct a Login object from serialized data.
dict
Serialized login data.
Login
Reconstructed Login object.

Utility Functions

get_emailconfirmation_model()

Returns the appropriate email confirmation model class based on settings.
type
Returns EmailConfirmation, EmailConfirmationHMAC, or raises NotImplementedError for code-based verification.

Usage Examples

Managing Email Addresses

Working with Confirmations

Login State Management