Request.user Gets the custom model

Source: Internet
Author: User
Tags auth

#models. PY Custom Model

Class MyUser (models. Model):
Username = models. Charfield (max_length=32, verbose_name=u ' username ')
Is_superuser = models. Booleanfield (Default=false, Verbose_name=u ' is super user ')
Is_active = models. Booleanfield (default=false, verbose_name=u ' activation ')
Password = models. Charfield (max_length=128, verbose_name=u ' password ')
Created = models. Datetimefield (auto_now_add=true, verbose_name=u ' creation time ')
Last_login= models. Datetimefield (auto_now=true, verbose_name=u ' Update Time ')

def __unicode__ (self):
Return Self.username

def is_authenticated (self):
Return True

def hashed_password (self, Password=none):
If not password:
Return Self.password
Else
return hashlib.md5 (password). Hexdigest ()

def check_password (self, password):
If Self.hashed_password (password) = = Self.password:
Return True
Return False

Class Meta:
Verbose_name = _ (U ' admin ')
Verbose_name_plural = _ (U ' admin ')

#auth. PY defines a back-end device to illustrate that the backend validation mechanism is used MyUser custom model

Class Mybackend:

Def authenticate (self, Username=none, Password=none):
Try
user = MyUser.objects.get (username=username)
Except Myuser.doesnotexist:
Pass
Else
If User.check_password (password):
return user
Return None

def get_user (self, user_id):
Try
Return MyUser.objects.get (pk=user_id)
Except Myuser.doesnotexist:
Return None


#settings. PY configuration back-end device
Authentication_backends = (
' Myapp.auth.MyBackend ',
)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.