Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Django1.5 Custom User Model     - OpenResty load balancing MySQL (Database)

- Talking about modern programming language syntax and standard library tightly bound phenomenon (Programming)

- Linux system security check method (Linux)

- Android Unzip the ZIP / GZIP data (based on the InflaterInputStream implementation) (Programming)

- High-performance JavaScript loops and flow control (Programming)

- Efficient running Linux virtual machine Six Tips (Linux)

- Linux operating system boot manager -GRUB (Linux)

- Java Generics (Programming)

- Spring multi data source configuration (Programming)

- Oracle rebuild index script (Database)

- CentOS7 installed MySQL (Database)

- How to install CentOS 7.x in OpenERP (Odoo) (Linux)

- learning Linux ls command examples (Linux)

- Use window.name + iframe cross-domain access to data Detailed (Programming)

- Ten SCP transfer command example (Linux)

- To compile and install MySQL 5.7.7 RC under CentOS 7.1 (Database)

- How to compile and install wxWidgets in Ubuntu / Debian / Linux Mint (Linux)

- Linux Thread Synchronization (Programming)

- Linux kernel compilation, the configuration of the motor drive (Programming)

- Ubucompilator-Ubuntu, Debian, Linux Mint created deb package of graphical tools (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

    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
     
         
         
         
  More:      
 
- Linux Network Programming - libnet Guide (Programming)
- MySQL 5.7.9 source compiler installation instructions (Database)
- Simple comparison of MySQL and Oracle in a sql analytical details (Database)
- Ubuntu 12.04 LTS installation configuration JDK1.6.0_45 (Linux)
- Oracle Data File Management (Database)
- ADSL router to defend their own network security methods (Linux)
- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)
- Ubuntu 14.04 install AMD graphics driver is fully dual monitor solution (Linux)
- Batch kill processes using awk command (Linux)
- On the design of Oracle database backup (Database)
- RedHat Linux 6.5 Enterprise Edition installation Redis 3.0.3 (Database)
- CentOS 6.5 system installation Oracle11.2.0.4 basic steps (Database)
- Ubuntu Server security risk checks (Linux)
- How to forcibly change the Linux system password on a regular basis (Linux)
- PXE + Kickstart automatically install CentOS 6.5 (Linux)
- Android Service service applications and the phone SMS Listener Listener (Programming)
- Observation network performance tools for Linux (Linux)
- Android design patterns - state mode (Programming)
- Definition Format Oracle basis of various statements (Database)
- Python 3 for instructions encoded string conversion (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.