【问题标题】:AssertionError with custom user model django-allauth自定义用户模型 django-allauth 的 AssertionError
【发布时间】:2013-09-22 04:41:17
【问题描述】:

我是 django 的新手,最近我决定集成 django allauth 以使用登录(facebook 和 google+),我使用自定义模型(django 1.5)和一对一的用户配置文件模型,我的自定义模型类看起来

class MyUser(AbstractBaseUser):   
   email = models.EmailField(
      verbose_name='email address',
      max_length=255,
      unique=True,
      db_index=True,
   )
   username = models.CharField(max_length=35,  blank=False)
   is_active = models.BooleanField (default=False)
   is_admin = models.BooleanField (default=False)

  objects = MyUserManager()

  USERNAME_FIELD = 'email'
  REQUIRED_FIELDS = ['username']

还有我的用户资料:

class UserProfile(models.Model):
   phone = models.FloatField(blank=True, null=True)
   is_visible = models.BooleanField (default=True)
   user = models.OneToOneField(MyUser, primary_key=True)

这里是 django allauth local_settings 文件:

ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_SIGNUP_FORM_CLASS = 'bloodi.accounts.forms.RegistrationForm'
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True

使用此配置,我遇到了 django allauth 的两个问题:
*) 它不允许重复的用户名 *) 注册过程中应用程序崩溃(似乎用户帐户必须处于活动状态才能继续登录)

AssertionError 
Exception Location: \allauth\account\utils.py in perform_login, line 110
...
assert user.is_active

【问题讨论】:

    标签: python django django-allauth


    【解决方案1】:

    Django 不允许使用重复的用户名,请参阅:

    https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD

    至于断言错误,使用:

    is_active = models.BooleanField(default=True)
    

    【讨论】:

    • 感谢您的快速响应,我希望该应用程序接受两个具有相同用户名但不使用电子邮件的用户,这就是为什么我使用 USERNAME_FIELD 作为“电子邮件”但注册 allauth 表单时出现错误“用户名已采取”,谢谢您的提前。
    • 好的,那么在您的项目中,您似乎没有真正的username,只有nickname 之类的东西。在这种情况下,将ACCOUNT_USER_MODEL_USERNAME_FIELD 设置为None ...
    • 谢谢!!很多,对于这个回应。
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2013-07-12
    • 2021-01-25
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多