Mix-in Model Classes

These classes are provided as tools to help build your own User models.

AbstractUser

class improved_user.model_mixins.AbstractUser(*args, **kwargs)[source]

Bases: improved_user.model_mixins.DjangoIntegrationMixin, improved_user.model_mixins.FullNameMixin, improved_user.model_mixins.ShortNameMixin, improved_user.model_mixins.EmailAuthMixin, django.contrib.auth.models.PermissionsMixin, django.contrib.auth.base_user.AbstractBaseUser

Abstract User base class to be inherited.

Do not instantiate this class directly. The class provides a fully featured User model with admin-compliant permissions. Differs from Django’s AbstractUser:

  1. Login occurs with an email and password instead of username.

  2. Provides short_name and full_name instead of first_name and last_name.

All fields other than email and password are optional.

Sets objects to UserManager.

Documentation about Django’s AbstractBaseUser may be helpful in understanding this class.

Parameters
  • date_joined (DateTimeField) – Date joined

  • email (EmailField) – Email address

  • full_name (CharField) – Full name

  • groups (ManyToManyField) – The groups this user belongs to. A user will get all permissions granted to each of their groups.

  • is_active (BooleanField) – Designates whether this user should be treated as active. Unselect this instead of deleting accounts.

  • is_staff (BooleanField) – Designates whether the user can log into the admin site.

  • is_superuser (BooleanField) – Designates that this user has all permissions without explicitly assigning them.

  • last_login (DateTimeField) – Last login

  • password (CharField) – Password

  • short_name (CharField) – Short name

  • user_permissions (ManyToManyField) – Specific permissions for this user.

check_password(raw_password)

Return a boolean of whether the raw_password was correct. Handles hashing formats behind the scenes.

clean()

Override default clean method to normalize email.

Call super().clean() if overriding.

clean_fields(exclude=None)

Clean all fields and raise a ValidationError containing a dict of all validation errors if any occur.

email_user(subject, message, from_email=None, **kwargs)

Send an email to this User.

full_clean(exclude=None, validate_unique=True)

Call clean_fields(), clean(), and validate_unique() on the model. Raise a ValidationError for any errors that occur.

get_deferred_fields()

Return a set containing names of deferred fields for this instance.

get_full_name()

Return the full name of the user.

get_group_permissions(obj=None)

Return a list of permission strings that this user has through their groups. Query all available auth backends. If an object is passed in, return only permissions matching this object.

get_session_auth_hash()

Return an HMAC of the password field.

get_short_name()

Return the short name for the user.

get_user_permissions(obj=None)

Return a list of permission strings that this user has directly. Query all available auth backends. If an object is passed in, return only permissions matching this object.

get_username()

Return the username for this User.

has_module_perms(app_label)

Return True if the user has any permissions in the given app label. Use similar logic as has_perm(), above.

has_perm(perm, obj=None)

Return True if the user has the specified permission. Query all available auth backends, but return immediately if any backend returns True. Thus, a user who has permission from a single auth backend is assumed to have permission in general. If an object is provided, check permissions for that object.

has_perms(perm_list, obj=None)

Return True if the user has each of the specified permissions. If object is passed, check if the user has all required perms for it.

has_usable_password()

Return False if set_unusable_password() has been called for this user.

property is_anonymous

Always return False. This is a way of comparing User objects to anonymous users.

property is_authenticated

Always return True. This is a way to tell if the user has been authenticated in templates.

refresh_from_db(using=None, fields=None)

Reload field values from the database.

By default, the reloading happens from the database this instance was loaded from, or by the read router if this instance wasn’t loaded from any database. The using parameter will override the default.

Fields can be used to specify which fields to reload. The fields should be an iterable of field attnames. If fields is None, then all non-deferred fields are reloaded.

When accessing deferred fields of an instance, the deferred loading of the field will call this method.

save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

save_base(raw=False, force_insert=False, force_update=False, using=None, update_fields=None)

Handle the parts of saving which should be done only once per save, yet need to be done in raw saves, too. This includes some sanity checks and signal sending.

The ‘raw’ argument is telling save_base not to save any parent models and not to do any changes to the values before save. This is used by fixture loading.

serializable_value(field_name)

Return the value of the field name for this instance. If the field is a foreign key, return the id value instead of the object. If there’s no Field object with this name on the model, return the model attribute’s value.

Used to serialize a field’s value (in the serializer, or form output, for example). Normally, you would just access the attribute directly and not use this method.

validate_unique(exclude=None)

Check unique constraints on the model and raise ValidationError if any failed.

DjangoIntegrationMixin

class improved_user.model_mixins.DjangoIntegrationMixin(*args, **kwargs)[source]

Mixin provides fields for Django integration to work correctly

Provides permissions for Django Admin integration, as well as date field used by authentication code.

Parameters
  • date_joined (DateTimeField) – Date joined

  • is_active (BooleanField) – Designates whether this user should be treated as active. Unselect this instead of deleting accounts.

  • is_staff (BooleanField) – Designates whether the user can log into the admin site.

EmailAuthMixin

class improved_user.model_mixins.EmailAuthMixin(*args, **kwargs)[source]

A mixin to use email as the username

Parameters

email (EmailField) – Email address

clean()[source]

Override default clean method to normalize email.

Call super().clean() if overriding.

email_user(subject, message, from_email=None, **kwargs)[source]

Send an email to this User.

FullNameMixin

class improved_user.model_mixins.FullNameMixin(*args, **kwargs)[source]

A mixin to provide an optional full name field

Parameters

full_name (CharField) – Full name

get_full_name()[source]

Return the full name of the user.

ShortNameMixin

class improved_user.model_mixins.ShortNameMixin(*args, **kwargs)[source]

A mixin to provide an optional short name field

Parameters

short_name (CharField) – Short name

get_short_name()[source]

Return the short name for the user.