简体   繁体   English

从 django-allauth 中删除“用户名”字段

[英]Remove 'username' field from django-allauth

When django-registration doesn't support django 1.5 and custom user model.当 django-registration 不支持 django 1.5 和自定义用户模型时。 I'm trying use django-allauth, from first look it's great product.我正在尝试使用 django-allauth,乍一看它是很棒的产品。

Problem i have - username field required, but in my app i don't have username's.我遇到的问题 - 需要用户名字段,但在我的应用程序中我没有用户名。 So, allauth documentation says:所以,allauth 文档说:

**Available settings:**
ACCOUNT_AUTHENTICATION_METHOD (="username" | "email" | "username_email")

Specifies the login method to use -- whether the user logs in by entering his username, e-mail address, or either one of both.指定要使用的登录方法——用户是通过输入用户名、电子邮件地址还是两者之一登录。

Ok, i done, and got error:好的,我完成了,但出现错误:

AssertionError at /accounts/signup/
No exception supplied

models.py:模型.py:

class MyUser(AbstractBaseUser, PermissionsMixin):
    title = models.CharField ('Name', max_length=100)
    email = models.EmailField('Email', max_length=255, unique=True)
    ...

settings.py设置.py

ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = ('email')
AUTH_USER_MODEL = 'internboard.MyUser'

What i'm doing wrong ?我在做什么错?

Thanks, i found, right settings for my task:谢谢,我找到了适合我的任务的正确设置:

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False

If you encounter the error django.core.exceptions.FieldDoesNotExist: Account has no field named 'username' with reference to USER_MODEL_USERNAME_FIELD in the stacktrace, you'll also need to set ACCOUNT_USER_MODEL_USERNAME_FIELD to None (or the appropriate field for your use case).如果您遇到错误django.core.exceptions.FieldDoesNotExist: Account has no field named 'username' with reference USER_MODEL_USERNAME_FIELD in the stacktrace,您还需要将ACCOUNT_USER_MODEL_USERNAME_FIELD设置为None (或适合您的用例的字段)。 The full set of settings needed are the following:所需的全套设置如下:

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False

This setting is explained in more detail in the django-allauth documentation for Custom User Models with defaults outlined in Configuration .此设置在自定义用户模型的 django-allauth 文档中有更详细的解释,默认设置在Configuration 中概述。

A probable cause could be any of the following for a custom user model;对于自定义用户模型,可能的原因可能是以下任何一种;

a) if you've totally eliminated "Username" as a field in your model, then, do ensure that in your model, you define the variable: a) 如果您已经完全消除了“用户名”作为模型中的字段,那么请确保在您的模型中定义变量:

               USERNAME_FIELD = 'email'

Also, do ensure that in the field definition of email, the field attribute;另外,请确保在电子邮件的字段定义中,字段属性;

              unique = True

is as well included.也包括在内。

b) In your custom User admin (which must be compulsorily configured), the form and add_form variables must be defined to inherit from "ModelForm" or UserCreationForm (for the add_form variable). b) 在您的自定义用户管理(必须强制配置)中,必须定义 form 和 add_form 变量以继承自“ModelForm”或 UserCreationForm(对于 add_form 变量)。

c) if you're inheriting allauth's Signup Form, then, you'll have to declare in your settings: c) 如果您继承了 allauth 的注册表单,那么您必须在设置中声明:

 ACCOUNT_USER_MODEL_USERNAME_FILED = None

Most importantly, ensure you write tests to ensure that form posts data and saves to the database.最重要的是,确保您编写测试以确保表单发布数据并保存到数据库。 Thanks.谢谢。 Hope someone finds this helpful..... Remember, it can only be hard, but never impossible!!!希望有人觉得这有帮助..... 请记住,这可能很难,但永远不可能!!! Happy 'Djangoing'....快乐'Djangoing'....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM