我创建了一个自定义的用户模型。 目前已经有在身份验证数据库用户。 所以,我使用的数据迁移到数据迁移到我的自定义用户模型。
这是我怎么没在自动迁移文件中的数据迁移(这是我从发现这里 ):
更新
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('userauth', '0002_auto_20150721_0605'),
]
operations = [
migrations.RunSQL('INSERT INTO userauth_userauth SELECT * FROM auth_user'),
migrations.RunSQL('INSERT INTO userauth_userauth_groups SELECT * FROM auth_user_groups'),
migrations.RunSQL('INSERT INTO userauth_userauth_user_permissions SELECT * FROM auth_user_user_permissions'),
]
models.py
class UserManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not username:
raise ValueError(_('The given username must be set'))
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=False,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
if not is_staff:
group = Group.objects.get(name='normal')
user.groups.add(group)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
user=self._create_user(username, email, password, True, True,
**extra_fields)
user.is_active=True
user.save(using=self._db)
return user
class UserAuth(AbstractBaseUser, PermissionsMixin):
#original fields
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), _('invalid'))
])
first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)
email = models.EmailField(_('email address'), max_length=255, unique=True)
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=False,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
#additional fields
full_name = models.CharField(_('Full Name'), max_length=600, blank=True, null=True)
profileImage = models.ImageField(upload_to="upload",blank=True,null=True,
help_text = _("Please upload your picture"))
BioUser = models.TextField(blank=True,null=True)
Social_link = models.URLField(blank=True,null=True)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_full_name(self):
return self.full_name
def get_short_name(self):
return self.first_name
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
迁移后的问题,我无法创建新用户。 我得到这个错误:
重复键值违反唯一约束“userauth_userauth_pkey”详细信息:按键(ID)=(3)已经存在。
似乎该表是不同步的。 我该如何解决?