在迁移中:AttributeError:使用自定义UserManager,“配置文件”对象没有属性“ set_password”

Django 3.0.3

我有应用account,可以通过migrate.py createsuperuser创建超级用户 这是models.py的一些内容:

class CustomUserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self,email,password,**extra_fields):
        email = self.normalize_email(email)
        user = self.model(email=email,**extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    ...

    def create_superuser(self,email=None,password=None,**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(email,**extra_fields)
class Profile(AbstractBaseUser,PermissionsMixin):
    email = models.EmailField(
        verbose_name='E-mail',unique=True,)
    first_name = models.CharField(verbose_name='first name',max_length=30,blank=True)
    last_name = models.CharField(verbose_name='last name',max_length=150,blank=True)

    ...

    objects = CustomUserManager()

    username_FIELD = 'email'

    class Meta:
        verbose_name = 'user'
        verbose_name_plural = 'users'

    ...

我正在尝试设置数据迁移。 首先,我运行manage.py makemigration,然后创建空迁移,这里是

def create_admin(apps,schema_editor):
    Profile = apps.get_model('accounts','Profile')
    Profile.objects.create_superuser(
        email='admin@mail.ru',password='123'
    )


class Migration(migrations.Migration):
    dependencies = [
        ('accounts','0001_initial'),]

    operations = [
        migrations.RunPython(create_admin),]

然后运行manage.py migrate并收到错误消息:AttributeError: 'Profile' object has no attribute 'set_password'

在settings.py中,我也有AUTH_USER_MODEL = 'accounts.Profile'

sunyinglu 回答:在迁移中:AttributeError:使用自定义UserManager,“配置文件”对象没有属性“ set_password”

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2608041.html

大家都在问