在django 1.8中将数据从原始用户模型迁移到自定义用户模型
我创建了一个自定义用户模型。auth数据库中已有个用户。因此,我使用数据迁移将数据迁移到我的自定义用户模型 这是我在自动迁移文件(我从中找到)中执行数据迁移的方式: 已更新在django 1.8中将数据从原始用户模型迁移到自定义用户模型,django,database-migration,django-migrations,Django,Database Migration,Django Migrations,我创建了一个自定义用户模型。auth数据库中已有个用户。因此,我使用数据迁移将数据迁移到我的自定义用户模型 这是我在自动迁移文件(我从中找到)中执行数据迁移的方式: 已更新 from __future__ import unicode_literals from django.db import models, migrations class Migration(migrations.Migration): dependencies = [ ('userauth
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\u userauth\u pkey”详细信息:密钥(id)=(3)已存在
这张桌子好像不同步了。如何修复此问题?您使用的是什么数据库?听起来您的数据库有一个主键计数器,该主键生成ID,如3。由于使用主键创建了新行,因此可能需要手动重置DB计数器。有关postgres中的一个示例,请参见postgres上的,Django处理为其数据库记录创建唯一主键的方法是使用从中获取新主键的。在我自己的数据库中,我看到如果我有一个名为x的表,那么Django将以x
\u id\u seq
的名称创建序列。因此,userauth\u userauth
表的顺序是userauth\u userauth\u id\u seq
现在,按照迁移的方式,您使用的是原始SQL语句。这完全绕过了Django的ORM,这意味着迁移没有触及新表的序列。执行这种原始迁移之后,您应该做的是将主键序列设置为一个不会与数据库中已有的序列冲突的数字。借用,则应发行:
select setval('userauth_userauth_id_seq', max(id))
from userauth_userauth;
并对其他表执行相同的操作:如果它们的id
字段为,则将它们自己的序列设置为最大值。(如果您想知道,将使用的下一个值将通过获取,并且将比调用nextval
之前的序列值多一个。)
在一篇评论中,你想知道为什么创建新用户最终会奏效。可能发生的情况是,您试图创建新用户,结果如下:
setval
所做的更改不会撤消
因此,最终,序列增加到超过表中已存在的最大主键,并从该点开始工作。能否显示模型自定义身份验证用户的代码?它是postrgres。但奇怪的是,当我今天尝试时,它起作用了!!别再犯错误了…我想不出为什么,你就是那个男人!!这就解释了为什么它现在起作用。我有测试时间来创建用户。你提到的顺序也随之增加。接得好@dev jim指出,将答案标记为正确并不等于奖励奖励。既然答案似乎对你有帮助,请考虑把它奖励给路易斯。