我目前在 Django 中将 is_active
字段从 False
设置回 True
时遇到问题。我有一个自定义帐户模型,它继承自 AbstractBaseUser
并由 class AccountManager(BaseUserManager)
管理。
默认情况下,我将 is_active
字段覆盖为 False。然而,当我尝试再次重新激活该字段时。它当时确实在 python manage.py shell 中更新,但是当我注销并重新登录到 shell 时。 is_active
字段仍为 False
。下面是我的 activateUser()
函数
def activateUser(self, account):
account.is_active = True
account.save()
return account
下面是我的python manage.py shell
执行:
>>> from authentication.models import Account
>>> a = Account.objects.all()[0]
>>> a.is_active
False
>>> Account.objects.activateUser(a)
>>> a.is_active
True
>>> exit()
python manage.py shell
>>> from authentication.models import Account
>>> a = Account.objects.all()[0]
>>> a.is_active
False
更新:这是我的帐户类(class)。上面的 activateUser()
函数来自 AccountManager
类,该类继承自 BaseUserManager
并管理 Account 类。
class Account(AbstractBaseUser):
"""
Create a new model from AbstractBaseUser account to have our own fields
"""
username = models.CharField(max_length=40, unique=True)
email = models.EmailField(unique=True)
firstName = models.CharField(max_length=40, blank=True, null=True)
lastName = models.CharField(max_length=40, blank=True, null=True)
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
is_staff = models.BooleanField(default=False)
is_active = False
objects = AccountManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def __unicode__(self):
return self.username
def get_full_name(self):
return self.firstName + ' ' + self.lastName
def get_short_name(self):
return self.firstName
最佳答案
没有地方可以保存 is_active
,因为您还没有为其分配数据库字段。
您应该将模型中的 is_active
更改为:
is_active = models.BooleanField(default=False)
关于python - 如何重新激活django用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30421267/