因此,我通过子类化AbstractBaseUser来向Django用户配置文件添加了一些新字段:
from __future__ import unicode_literals
from datetime import timedelta
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.conf import settings
from django_countries.fields import CountryField
from .managers import CustomUserManager
BOOLEAN_YN = (
(True, u'Yes'),
(False, u'No'),
)
class UserProfile(AbstractUser):
full_name = models.CharField(_("fullname"),max_length=100,blank=True,null=True)
timezone = models.CharField(_("time zone"), max_length=32, default='UTC',null=True,blank=True)
is_active = models.BooleanField(_("activated"),default=False)
is_verified = models.BooleanField(
_('verified'), default=False,
help_text=_(
'Designates whether the user has verified his '
'account by email or by other means. Un-select this '
'to let the user activate his account.'))
position = models.CharField(max_length=100,null=True,blank=True)
institution = models.CharField(max_length=100, null=True, blank=True)
resident_country = CountryField(null=True, blank=True)
home_country = CountryField(null=True, blank=True)
introduction = models.TextField(max_length=1500,blank=True,null=True)
gdpr = models.BooleanField(default=True,choices=BOOLEAN_YN)
email_contact = models.BooleanField(default=True,choices=BOOLEAN_YN)
REQUIRED_FIELDS = ['email']
objects = CustomUserManager()
def __str__(self):
return self.first_name + ' ' + self.last_name
class Meta:
verbose_name = _("User profile")
verbose_name_plural = _("user profiles")
def save(self, *args, **kwargs):
if self.is_superuser:
self.is_administrator = True
self.full_name = self.first_name + ' ' + self.last_name
super(UserProfile, self).save(*args, **kwargs)
很显然,这导致了以下问题:python manage.py createsuperuser
没有创建超级用户。
我是这样在manager.py中创建CustomUserManager的:
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
class CustomUserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifiers
for authentication instead of usernames.
"""
def create_user(self, email, password, **extra_fields):
"""
Create and save a User with the given email and password.
"""
if not email:
raise ValueError(_('The Email must be set'))
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
"""
Create and save a SuperUser with the given email and password.
"""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('is_verified',True)
extra_fields.setdefault('gdpr', True)
extra_fields.setdefault('email_contact',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(email, password, **extra_fields)
执行此操作,然后当然运行适当的迁移等。...我可以在本地运行createsuperuser
命令,不会出现任何问题并获得超级用户。
但是,当我部署到Heroku然后运行:heroku run python manage.py createsuperuser
时,我收到一条消息,说明已创建了超级用户,但是当我登录时,得到的用户名/密码错误 >错误。
有趣的是,我尝试只在本地创建超级用户,然后将数据库(带有用户信息)推送到Heroku。我尝试登录,但仍然出现不正确的用户名/密码错误。看来用户存在,但是,它不接受用户密码!
有人对可能发生的事情有任何想法吗?
谢谢