Django重写auth_user表 -- 2019-08-08 18:03:33

Posted gqy02

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django重写auth_user表 -- 2019-08-08 18:03:33相关的知识,希望对你有一定的参考价值。

原文: http://106.13.73.98/__/151/

from django.db import models
from django.contrib import auth
from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, username, password, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        if not username:
            raise ValueError('The given username must be set')
        username = self.normalize_email(username)
        username = self.model.normalize_username(username)
        user = self.model(username=username, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(username, password, **extra_fields)

    def create_superuser(self, username, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self._create_user(username, password, **extra_fields)


# A few helper functions for common logic between User and AnonymousUser.
def _user_get_all_permissions(user, obj):
    permissions = set()
    for backend in auth.get_backends():
        if hasattr(backend, "get_all_permissions"):
            permissions.update(backend.get_all_permissions(user, obj))
    return permissions


def _user_has_perm(user, perm, obj):
    """
    A backend can raise `PermissionDenied` to short-circuit permission checking.
    """
    for backend in auth.get_backends():
        if not hasattr(backend, 'has_perm'):
            continue
        try:
            if backend.has_perm(user, perm, obj):
                return True
        except PermissionDenied:
            return False
    return False


def _user_has_module_perms(user, app_label):
    """
    A backend can raise `PermissionDenied` to short-circuit permission checking.
    """
    for backend in auth.get_backends():
        if not hasattr(backend, 'has_module_perms'):
            continue
        try:
            if backend.has_module_perms(user, app_label):
                return True
        except PermissionDenied:
            return False
    return False


# ========================================================================================================================
# =====================================其它代码可以不管,更改下面的字段等信息就可以了==============================================
# ========================================================================================================================

class UserProfile(AbstractBaseUser, PermissionsMixin):
    username = models.CharField("用户名", max_length=32, unique=True)
    is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("是否可以登录到管理站点"))
    is_active = models.BooleanField(default=True, help_text=("是否激活"))
    nickname = models.CharField("昵称", max_length=32)
    avatar = models.ImageField("头像", upload_to="UA", default='UA/default.jpg')
    gender = models.CharField("性别", choices=(("male", "男"), ("female", "女")), max_length=8, null=True, blank=True)
    birthday = models.DateField("生日", null=True, blank=True, help_text="日期格式:YYYY-MM-DD,相当于Python中的datetime.date()实例")
    email = models.EmailField("邮箱", max_length=255, null=True, blank=True)  # EmailField:字符串类型,Django Admin以及ModelForm中提供验证机制
    date_joined = models.DateTimeField("加入日期", auto_now_add=True)

    USERNAME_FIELD = 'username'  # 你必须指定用户名
    # REQUIRED_FIELDS = []  # 还可以指定必填字段

    class Meta:
        db_table = 'user'  # 指定数据库中的表名称
        verbose_name_plural = '用户信息'  # 管理站点中显示的表名称


# 迁移数据后,你会发现数据库表字段中的前3个字段为:password、last_login、is_superuser
# 不必惊慌,这是Django做的,认证系统中会用到这3个字段

# 最后,在settings.py文件中指定此类:
    # AUTH_USER_MODEL = 'App名称.UserProfile'

# ========================================================================================================================
# ========================================================================================================================


    def __str__(self):  # __unicode__ on Python 2
        return self.username  # 该返回值会作为认证方法authenticate()的返回值

    def get_full_name(self):
        # The user is identified by their email address
        return self.nickname

    def get_short_name(self):
        # The user is identified by their email address
        return self.username

    def has_perm(self, perm, obj=None):
        #     "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always

        if self.is_active and self.is_superuser:
            return True
        return _user_has_perm(self, perm, obj)

    def has_perms(self, perm_list, obj=None):
        #     "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        for perm in perm_list:
            if not self.has_perm(perm, obj):
                return False
        return True

    def has_module_perms(self, app_label):
        #     "Does the user have permissions to view the app `app_label`?"
        #     Simplest possible answer: Yes, always
        if self.is_active and self.is_superuser:
            return True

        return _user_has_module_perms(self, app_label)

    objects = UserManager()

数据迁移后,我们查看表结构如下:

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| password     | varchar(128) | NO   |     | NULL    |                |   # (密码)
| last_login   | datetime(6)  | YES  |     | NULL    |                |   # (最后一次登录时间)
| is_superuser | tinyint(1)   | NO   |     | NULL    |                |   # (是否为超级用户)
| username     | varchar(32)  | NO   | UNI | NULL    |                |
| is_staff     | tinyint(1)   | NO   |     | NULL    |                |
| is_active    | tinyint(1)   | NO   |     | NULL    |                |
| nickname     | varchar(32)  | NO   |     | NULL    |                |
| avatar       | varchar(100) | NO   |     | NULL    |                |
| gender       | varchar(8)   | YES  |     | NULL    |                |
| birthday     | date         | YES  |     | NULL    |                |
| email        | varchar(255) | YES  |     | NULL    |                |
| date_joined  | datetime(6)  | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

原文: http://106.13.73.98/__/151/

以上是关于Django重写auth_user表 -- 2019-08-08 18:03:33的主要内容,如果未能解决你的问题,请参考以下文章

Django-Auth模块之auth_user表

在 django 中将自定义字段添加到 auth_user 表

Django框架之admin管理后台

Django的坑

django的RBAC认证z;自定义auth_user表;认证组件权限组件源码分析;认证组件;权限组件

如何使用 django 向默认 auth_user 模型添加额外的模型字段