默认情况下创建用户不活动(is_active default False) [英] Create user inactive as default (is_active default False)
问题描述
我在我的网站上使用了Facebook身份验证,我使用 omab / django-social-auth
I have facebook authentication in my website which I use omab / django-social-auth
我想将用户重定向到另一个网站,让他们填写他们的详细信息。所以我想让用户在他们的脸书帐户首次认证后不起作用,然后在完成表单后将其保存为活动用户。
I want to redirect users to another website to make them fill their details. So I want to have users as inactive when they first authenticate with their facebook accounts, then after they complete the form I save them as active users.
我在我的环境中操作了django / contrib / auth / models.py,就像is_active字段一样default = False;但是它们保存为活动用户
,但仍然保持相同的结果,即使我从管理面板添加了一个普通用户。有没有我失踪的东西?
I manipulated the django/contrib/auth/models.py under my enviroment as with is_active fields as default=False; but they are saved as active user but still the same result, even I add a normal user from the admin panel. Is there something I am missing?
class User(models.Model):
"""
Users within the Django authentication system are represented by this
model.
Username and password are required. Other fields are optional.
"""
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
'@/./+/-/_ characters'))
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('e-mail address'), blank=True)
password = models.CharField(_('password'), max_length=128)
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.'))
is_superuser = models.BooleanField(_('superuser status'), default=False,
help_text=_('Designates that this user has all permissions without '
'explicitly assigning them.'))
last_login = models.DateTimeField(_('last login'), default=timezone.now)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
groups = models.ManyToManyField(Group, verbose_name=_('groups'),
blank=True, help_text=_('The groups this user belongs to. A user will '
'get all permissions granted to each of '
'his/her group.'))
user_permissions = models.ManyToManyField(Permission,
verbose_name=_('user permissions'), blank=True,
help_text='Specific permissions for this user.')
objects = UserManager()
def create_user(self, username, email=None, password=None):
"""
Creates and saves a User with the given username, email and password.
"""
now = timezone.now()
if not username:
raise ValueError('The given username must be set')
email = UserManager.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=False, is_active=False, is_superuser=False,
last_login=now, date_joined=now)
user.set_password(password)
user.save(using=self._db)
return user
推荐答案
- 避免修改内置函数,有更好的方法来处理。
- 像信号,信号很棒。
- 在这种情况下,我会附加到
django.contrib.auth.models.User
的pre_save信号,并手动更正is_active
模型实例的属性(如果对象是新的)。 - 这个w ay,您可以添加一些逻辑,以确保您正确地将用户标记为不活动。
- 由于管理员中添加的用户应该处于活动状态,如果管理员将其标记为活动状态。
- Avoid modifying built-ins. There are better ways to do things.
- Like signals. Signals are awesome.
- In this case, I'd attach to the pre_save signal of
django.contrib.auth.models.User
, and manually correct theis_active
property of the model instance (if the object is new). - This way, you can add some logic to make sure you're properly marking a user as not active.
- Because user's added within the Admin should probably be active, if the admin marks them as active.
这篇关于默认情况下创建用户不活动(is_active default False)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!