Skip to main content

UserManager

This class provides helper methods for creating and managing user and superuser accounts, supporting both synchronous and asynchronous operations. It handles the normalization of usernames and emails, password hashing, and the configuration of default permission flags. Additionally, it includes functionality to query users based on specific permissions through configured authentication backends.

Attributes

AttributeTypeDescription
use_in_migrationsboolean = TrueIndicates whether the manager should be serialized into migrations and available on the historical models.

Methods


create_user()

@classmethod
def create_user(
username: string,
email: string = None,
password: string = None,
**extra_fields: dict
) - > [User](user.md?sid=django_contrib_auth_models_user)

Creates, saves, and returns a standard user with is_staff and is_superuser set to False by default.

Parameters

NameTypeDescription
usernamestringThe unique identifier for the user account.
emailstring = NoneThe email address for the user.
passwordstring = NoneThe raw password string to be hashed.
**extra_fieldsdictAdditional attributes to save on the user record.

Returns

TypeDescription
[User](user.md?sid=django_contrib_auth_models_user)The saved User instance.

acreate_user()

@classmethod
def acreate_user(
username: string,
email: string = None,
password: string = None,
**extra_fields: dict
) - > [User](user.md?sid=django_contrib_auth_models_user)

Asynchronously creates, saves, and returns a standard user with is_staff and is_superuser set to False by default.

Parameters

NameTypeDescription
usernamestringThe unique identifier for the user account.
emailstring = NoneThe email address for the user.
passwordstring = NoneThe raw password string to be hashed.
**extra_fieldsdictAdditional attributes to save on the user record.

Returns

TypeDescription
[User](user.md?sid=django_contrib_auth_models_user)A coroutine resolving to the saved User instance.

create_superuser()

@classmethod
def create_superuser(
username: string,
email: string = None,
password: string = None,
**extra_fields: dict
) - > [User](user.md?sid=django_contrib_auth_models_user)

Creates, saves, and returns a user with administrative privileges, ensuring is_staff and is_superuser are both True.

Parameters

NameTypeDescription
usernamestringThe unique identifier for the superuser account.
emailstring = NoneThe email address for the superuser.
passwordstring = NoneThe raw password string to be hashed.
**extra_fieldsdictAdditional attributes to save on the superuser record.

Returns

TypeDescription
[User](user.md?sid=django_contrib_auth_models_user)The saved superuser instance.

acreate_superuser()

@classmethod
def acreate_superuser(
username: string,
email: string = None,
password: string = None,
**extra_fields: dict
) - > [User](user.md?sid=django_contrib_auth_models_user)

Asynchronously creates, saves, and returns a user with administrative privileges, ensuring is_staff and is_superuser are both True.

Parameters

NameTypeDescription
usernamestringThe unique identifier for the superuser account.
emailstring = NoneThe email address for the superuser.
passwordstring = NoneThe raw password string to be hashed.
**extra_fieldsdictAdditional attributes to save on the superuser record.

Returns

TypeDescription
[User](user.md?sid=django_contrib_auth_models_user)A coroutine resolving to the saved superuser instance.

with_perm()

@classmethod
def with_perm(
perm: string,
is_active: boolean = True,
include_superusers: boolean = True,
backend: string = None,
obj: object = None
) - > [QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)

Returns a queryset of users who possess the specified permission, optionally filtering by active status and backend.

Parameters

NameTypeDescription
permstringThe permission codename to filter users by.
is_activeboolean = TrueWhether to include only active users in the results.
include_superusersboolean = TrueWhether to automatically include superusers, who typically have all permissions.
backendstring = NoneThe dotted import path of the authentication backend to use for the permission check.
objobject = NoneAn optional model instance for checking object-level permissions.

Returns

TypeDescription
[QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)A queryset containing users that match the permission and status criteria.