https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model
Substituting a custom User
model
django allows override the default user mode, a setting value is provided to point to customized model,
= AUTH_USER_MODEL 'users.User' #users app name is #User user model
Using a custom user model when starting a project
If you start a new project, set a custom user model is more worthy of recommendation, even if the default user model has to meet the demand, the model and the default user model is the same, but you can make when you need to customize it.
from django.contrib.auth.models import AbstractUser class User(AbstractUser): pass
Do not forget to let AUTH_USER_MODEL point to it, remember to create or run any migrations migrate
do this thing before.
In addition, this model registered in the app in admin.py
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from .models import User admin.site.register(User, UserAdmin)
Specifying a custom user model
All the information related to the user in a model, to avoid the relevant models for complex queries.
If you use the default authentication backend, your model must have a unique field for the identification, may be a username, or email address, or other unique key.
Build custom user model The easiest way is from a compatible AbstractBaseUser
. AbstractBaseUser
Inherited, provides the core of the realization of user model. Including password hashes and password reset token of.
Some implementations on the details:
the MyUser class (AbstractBaseUser): identifier = models.CharField (= 40 MAX_LENGTH, UNIQUE = True) ... USERNAME_FIELD = 'identifier' # description string of the field name is used as a unique identifier of the user model. This is usually some sort of user name, but can also be e-mail address or any other unique identifier.
MyUser class (AbstractBaseUser): ... date_of_birth = models.DateField () height = models.FloatField () ... required_fields = [ 'date_of_birth', 'height'] # createsuperuser management through command prompt when you create a user name field list. The system will prompt the user to provide values for each field.
is_active
A Boolean property that indicates whether the user is considered "active." This attribute as AbstractBaseUser
the default to provide property True
.
The following properties and can be used for any of the following methods AbstractBaseUser的子类
:
set_password
(raw_password)
Save to the original password for the password hash
Writing a manager for a custom user model
You should also model for your user-defined custom manager. If your user model only defines username
, , email
, is_staff
, is_active
, is_superuser
, last_login
and date_joined
fields, and Django's default, as you can only install the Django ; however, if your user model defines different fields, you need to define a custom manager and expand the offer of two other methods:UserManager
BaseUserManager
class models.
CustomUserManager
create_user()的原型
Should accept the User Name field, plus all the required fields as parameters. For example, if your user model usedemail作为用户名字段
, and date_of_birth作为
required fields, create_user
it should be defined as:
def create_user(self, email, date_of_birth, password=None): # create user here ...