在Django中创建超级用户

时间:2018-07-10 12:48:53

标签: django authentication django-models

我如下所示修改了用户模型,以使用户通过电话登录。

from django.db import models
import datetime
from django.core.mail import send_mail
from django.contrib.auth.models import (
    AbstractBaseUser, BaseUserManager
)
from django.contrib.staticfiles.templatetags.staticfiles import static

from django.core.validators import RegexValidator



class UserManager(BaseUserManager):

def get_by_natural_key(self, username):
    case_insensitive_username_field = '{}__iexact'.format(self.model.USERNAME_FIELD)
    return self.get(**{case_insensitive_username_field: username})

def create_user(self,phone_number,password=None,is_staff=False,is_active = True, is_admin = False, is_vendor = False):

    if not phone_number:
        raise ValueError("Users Must Have a phone number")
    if not password:
        raise ValueError("Users must have a password")
    user = self.model(
        phone_number )

    user.set_password(password)
    user.is_staff = is_staff
    user.is_active = is_active
    user.is_admin = is_admin
    user.is_vendor = is_vendor
    user.save(using=self._db)
    return user

def create_staffuser(self,phone_number, password=None):
    user = self.create_user(
    phone_number,
    password=password,
    is_staff=True
    )
    return user
def create_superuser(self,phone_number, password=None):
    user = self.create_user(
    phone_number,
    password=password,
    is_admin=True,
    is_staff=True
    )
    return user
def create_vendoruser(self,phone_number, password=None):
    user = self.create_user(
    phone_number,
    password=password,
    is_vendor=True
    )
    return user



class User(AbstractBaseUser):
    phone_number = models.CharField(max_length=20, unique=True)
    date_joined = models.DateField(auto_now_add = True)
    is_active = models.BooleanField(default = True)
    is_staff = models.BooleanField(default = False)
    is_admin = models.BooleanField(default = False)
    is_vendor = models.BooleanField(default = False)
    is_basic = models.BooleanField(default = False)

 REQUIRED_FIELDS = []

objects = UserManager()

def __str__(self):
    return self.phone_number

# def save(self, *args, **kwargs):
#     self.email = self.email.lower()
#     return super(User, self).save(*args, **kwargs)

def get_full_name(self):
    return self.phone_number

def get_short_name(self):
    return self.phone_number

def has_perm(self, perm, obj=None):
    return True

def has_module_perms(self, app_label):
    return True

def get_type(self):
    if self.vendor:
        return self.vendor_user
    else:
        return self.basic_user

@property
def staff(self):
    return self.is_staff

@property
def admin(self):
    return self.is_admin

@property
def active(self):
    return self.is_active

@property
def vendor(self):
    return self.is_vendor

@property
def basic(self):
    return self.is_basic




USERNAME_FIELD = 'phone_number'

然后,我创建了一个超级用户并尝试登录,但是无法登录。 从python shell检查数据库后,我意识到该数据库为我创建的超级用户提供了一个空电话号码字段。

问题是:创建超级用户时电话号码无法正确保存

1 个答案:

答案 0 :(得分:2)

在您的代码中编写:

user = self.model(phone_number)

(重新格式化)

但是,如果您创建模型实例,则所有参数都必须使用命名参数(由于字段是 unordered ,因此很难知道这些值将在哪里“ land ”)。因此,您必须指定要更新的字段名称:

user = self.model(phone_number=phone_number)