Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Django1.5 Custom User Model     - Getting Started with Linux system to learn: How do I know which processes are running on the CPU core (Linux)

- Linux System shutdown procedures (Linux)

- df show disk space usage (Linux)

- Linux Security trust with users (Linux)

- Python several standard types of built-in functions (Programming)

- Install apr support for Tomcat on Linux (Server)

- About MongoDB query method according to fuzzy field (Database)

- Linux Change ssh port and disable remote root login at (Linux)

- Search Linux commands and files - which, whereis, locate, find (Linux)

- Manually generate AWR reports (Database)

- How to configure security services under Linux (Linux)

- Android custom ViewPager create kaleidoscopic image transition effects (Programming)

- CentOS install SystemTap-2.6 (Linux)

- Debian (Wheezy) Install Redmine 2.6 (Programming)

- Hadoop vs spark (Server)

- Revive Adserver ad server installation on Ubuntu 15.04 / CentOS7 (Server)

- In-depth understanding of PHP ini configuration (Server)

- Redhat 7 modify the default run level method --RHEL7 use systemd to create a symbolic link to the default runlevel (Linux)

- Arronax allows you to easily create desktop startup file (Linux)

- JBPM6 Tutorial - Fast Fun JBPM table (Linux)

  Django1.5 Custom User Model
  Add Date : 2017-04-13      
  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


    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'
    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

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
- CentOS build JDK environment (Linux)
- How to install Perl modules from CPAN (Linux)
- Memcache explain in detail (Server)
- Linux Operating System Security Study (Linux)
- Wine 1.7 is installed on a system based on RedHat or Debian (Linux)
- Android Studio Personalization (Linux)
- MongoDB start under Linux (Database)
- Tomcat session clustering and server session (Server)
- 32-bit and 64-bit integer in PHP and MySQL range (Database)
- Android HTTP request with Get Information (Programming)
- SpringMVC garbage processing (Programming)
- There are more than one server, there is reason to try cloud operating system (Server)
- WinSCP to transfer text files will automatically convert the format (Linux)
- MySQL use the integer type (Database)
- Spark and Hadoop comparison (Server)
- Oracle Incident Packaging Service (Database)
- Ubuntu install Eclipse for C / C ++ and related configuration (Linux)
- Ubuntu deploying Solr (4.4) to Tomcat (7.0.53) (Server)
- To modify the existing user ID and comments GPG key (Linux)
- Oracle 11g new features and associated SQL TUNING (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.