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
| Attribute | Type | Description |
|---|
| use_in_migrations | boolean = True | Indicates 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
| Name | Type | Description |
|---|
| username | string | The unique identifier for the user account. |
| email | string = None | The email address for the user. |
| password | string = None | The raw password string to be hashed. |
| **extra_fields | dict | Additional attributes to save on the user record. |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| username | string | The unique identifier for the user account. |
| email | string = None | The email address for the user. |
| password | string = None | The raw password string to be hashed. |
| **extra_fields | dict | Additional attributes to save on the user record. |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| username | string | The unique identifier for the superuser account. |
| email | string = None | The email address for the superuser. |
| password | string = None | The raw password string to be hashed. |
| **extra_fields | dict | Additional attributes to save on the superuser record. |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| username | string | The unique identifier for the superuser account. |
| email | string = None | The email address for the superuser. |
| password | string = None | The raw password string to be hashed. |
| **extra_fields | dict | Additional attributes to save on the superuser record. |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| perm | string | The permission codename to filter users by. |
| is_active | boolean = True | Whether to include only active users in the results. |
| include_superusers | boolean = True | Whether to automatically include superusers, who typically have all permissions. |
| backend | string = None | The dotted import path of the authentication backend to use for the permission check. |
| obj | object = None | An optional model instance for checking object-level permissions. |
Returns
| Type | Description |
|---|
[QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset) | A queryset containing users that match the permission and status criteria. |