Django:无法解析/ accounts / password_reset /上的关键字“is_active”

时间:2018-05-30 13:49:11

标签: django

我正在尝试使用我的自定义用户模型实现Django的内置密码重置功能,但当我尝试在/ accounts / password_reset /提交电子邮件地址时遇到此错误:

FieldError at /accounts/password_reset/
Cannot resolve keyword 'is_active' into field. Choices are: ...

例外位置:

Exception Location: C:\Users\...\.virtualenvs\my_project-IsNTW6sC\lib\site-packages\django\db\models\sql\query.py in names_to_path, line 1378

如果有人想看到它,我可以提供完整的回溯,但它真的很长,而且都指向后端的Django文件。

这是 models.py:

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, is_staff=False, is_admin=False, is_active=True):
        if not email:
            raise ValueError('Users must have an email address.')
        if not password:
            raise ValueError('Users must have a password.')

        user = self.model(
                email = self.normalize_email(email)
            )
        user.staff = is_staff
        user.admin = is_admin
        user.active = is_active
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_staffuser(self, email, password=None):
        user = self.create_user(
                email,
                password=password,
                is_staff=True
            )
        return user

    def create_superuser(self, email, password=None):
        user = self.create_user(
                email,
                password=password,
                is_staff=True,
                is_admin=True
            )
        return user



class User(AbstractBaseUser):
    email = models.EmailField(max_length=254, unique=True)
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [] # USERNAME_field (email) and pw required by default
    objects = UserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

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

    def has_module_perms(self, app_label):
        return True

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

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

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



class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    username = models.CharField(max_length=200, unique=True, blank=False, validators=[
        RegexValidator(
            regex='^[a-z0-9_-]*$',
            message='Usernames can only contain letters, numbers, underscores, and dashes.'
            )
        ])
    first_name = models.CharField(max_length=200, blank=False)
    last_name = models.CharField(max_length=200, blank=False)
    city = models.CharField(max_length=30, blank=True)
    state_or_province = models.CharField(max_length=2, choices=all_choices, default='')
    bio = models.TextField(max_length=500, blank=True)
    date_joined = models.DateField(auto_now_add=True)
    cc_reference = models.CharField(max_length=200, default=False)
    event_list = ArrayField(models.IntegerField(), default=list)

    def _get_username(self):
        username = self.user.email
        return username

    def _get_firstname(self):
        firstname = self.user.email
        return firstname

    def _get_lastname(self):
        lastname = self.user.email
        return lastname

    def save(self, *args, **kwargs):
        if not self.username:
            self.username = self._get_username()
            self.first_name = self._get_firstname()
            self.last_name = self._get_lastname()
        super().save()

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
    instance.userprofile.save()

感谢任何帮助。谢谢!

修改

感谢下面的答案,我修复了密码重置问题。但是,我现在无法将超级用户登录到管理页面。新用户模型如下。我没有更改UserManager:

class User(AbstractBaseUser):
    email = models.EmailField(max_length=254, unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [] # USERNAME_field (email) and pw required by default
    objects = UserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

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

    def has_module_perms(self, app_label):
        return True

4 个答案:

答案 0 :(得分:1)

MyUser类中,可能有三个功能:

def is_staff(self):
    return self.is_staff

def is_admin(self):
    return self.is_admin

def is_active(self):
    return self.is_active

我相信这些名称的重复会产生冲突。

  • 如果可能,请从您的自定义staff中删除User
  • 删除后继功能:不是必需的(注意:仅功能,不是对象)
   def is_active(self):
        return self.is_active
  • 重命名u / m函数(u可以使用is_staff(self)调用is_admin:
   def is_staff(self):
       return self.is_admin

这将清除冲突,并且两个问题都将得到解决。

注意:您还必须从createstaffuser()类中删除MyUserManager函数,还必须更改adminactive(不论您在何处使用它们)都分别移至is_activeis_active。最后,您必须从管理员,表单和模型中删除人员。

我希望这会有所帮助。

答案 1 :(得分:1)

就我而言,我刚刚将字段active重命名为is_active,并删除了is_active @property函数。 然后,当我makemigrations询问我是否要将active字段重命名为is_active时,我按下了Y进行了迁移。 最后,我migrate对数据库所做的更改。 它的工作没有任何错误。

答案 2 :(得分:0)

您的模型定义了字段activeadminstaff,而不是is_adminis_staffis_active

答案 3 :(得分:0)

尝试将is_superuser字段添加到用户模型中,然后迁移更改。

之后,使用terminal / cmd创建超级用户,然后尝试以超级用户agian身份登录