|
Django1.5 custom user profile can be described as a lot of simple, write your own model classes MyUser, MyUser at least meet the following requirements:
There must be an integer primary key
There is a unique constraint fields, such as username or email, used for user authentication
Provides a method to "short" and "long" form displays user, another way is to achieve getfullname and getshortname methods.
One: Create an account app in the project
django-admin startapp account
Two: Customize MyUser
Implementing Custom User Model The easiest way is to inherit AbstractBaseUser, AbstractBaseUser implements the core functionality of User, you need some additional details to achieve it. AbstractBaseUser can look at the source code:
@ Python_2_unicode_compatible
class AbstractBaseUser (models.Model):
password = models.CharField (_ ( 'password'), max_length = 128)
last_login = models.DateTimeField (_ ( 'last login'), default = timezone.now)
is_active = True
REQUIRED_FIELDS = []
class Meta:
abstract = True
def get_username (self):
"Return the identifying username for this User"
return getattr (self, self.USERNAME_FIELD)
def __str __ (self):
return self.get_username ()
def natural_key (self):
return (self.get_username (),)
def is_anonymous (self):
"" "
Always returns False. This is a way of comparing User objects to
anonymous users.
"" "
return False
def is_authenticated (self):
"" "
Always return True. This is a way to tell if the user has been
authenticated in templates.
"" "
return True
def set_password (self, raw_password):
self.password = make_password (raw_password)
def check_password (self, raw_password):
"" "
Returns a boolean of whether the raw_password was correct. Handles
hashing formats behind the scenes.
"" "
def setter (raw_password):
self.set_password (raw_password)
self.save (update_fields = [ "password"])
return check_password (raw_password, self.password, setter)
def set_unusable_password (self):
# Sets a value that will never be a valid hash
self.password = make_password (None)
def has_usable_password (self):
return is_password_usable (self.password)
def get_full_name (self):
raise NotImplementedError ()
def get_short_name (self):
raise NotImplementedError ()
AbstractBaseUser only getfullname and getshortname method is not realized. Then we inherit AbstractBaseUser customize the User model called MyUser:
class MyUser (AbstractBaseUser, PermissionsMixin):
username = models.CharField ( 'username', max_length = 30, unique = True,
db_index = True)
email = models.EmailField ( 'email address', max_length = 254, unique = True)
date_of_birth = models.DateField ( 'date of birth', blank = True, null = True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = [ 'username']
is_staff = models.BooleanField ( 'staff status', default = False,
help_text = 'Designates whether the user can log into this admin'
'Site.')
is_active = models.BooleanField ( 'active', default = True,
help_text = 'Designates whether this user should be treated as'
'Active. Unselect this instead of deleting accounts.')
def get_full_name (self):
full_name = '% s% s'% (self.first_name, self.last_name)
return full_name.strip ()
def get_short_name (self):
return self.first_name
objects = MyUserManager ()
USERNAME_FIELD: As a user login authentication field can usename, or email, etc., but must be unique.
REQUIRED_FIELDS: field operator entered when prompted to create a superuser command createsuperuser
is_staff: determine whether the user can log management background
is_active: determine whether the user can log in
Three: Custom MyUserManager
To customize a while as a manager MyUser, through inheritance BaseUserManager, and provide creat_user create_superuser methods.
class MyUserManager (BaseUserManager):
def create_user (self, username, email = None, password = None, ** extra_fields):
now = timezone.now ()
if not email:
raise ValueError ( 'The given email must be set')
email = UserManager.normalize_email (email)
user = self.model (username = username, email = email,
is_staff = False, is_active = True, is_superuser = False,
last_login = now, ** extra_fields)
user.set_password (password)
user.save (using = self._db)
return user
def create_superuser (self, username, email, password, ** extra_fields):
u = self.create_user (username, email, password, ** extra_fields)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save (using = self._db)
return u
Four: Specifies AUTHUSERMODEL
Override the default AUTHUSERMODEL, increase in settings.py file: AUTHUSERMODEL = 'user.MyUser'
Five: Sign MyUser
Create admin.py in account module, add the following code to register MyUser model to the admin:
from django.contrib import admin
from user.models import MyUser
admin.site.register (MyUser)
Summary: User model to achieve a custom in Django1.5 simple enough, according to their needs inheritance AbstractBaseUser it. Of course, if you want to learn more about Django model custom user content, the official documentation tells you more and better intact |
|
|
|