【问题标题】:Avoid username field creation in custom user model for django allauth避免在 django allauth 的自定义用户模型中创建用户名字段
【发布时间】:2020-05-29 06:51:20
【问题描述】:

我正在使用带有 allauth 的自定义用户模型,我需要省略用户名字段。我已经看过文档和一大堆关于使用 ACCOUNT_USER_MODEL_USERNAME_FIELD = None 的 stackoverflow 答案,但所有这些仍然导致我的数据库有一个用户名字段。

现在由于 db 仍然有一个 username 字段并在 上设置了唯一约束,allauth 不会将用户名放入上述设置为 None 的字段中,这导致我在第一个用户创建后面对IntegrityError。我知道我可以通过将上述设置设置为'username' 来解决这个问题,但我很好奇,我怎么不做用户名,因为我从不使用它。

我的模特:

class CustomUser(AbstractUser):
    # Custom user model for django-allauth
    first_name = None
    last_name = None

    def delete(self):
        # Custom delete - make sure user storage is also purged
        # Purge the user storage
        purge_userstore(self.email)
        # Call the original delete method to take care of everything else
        super(CustomUser, self).delete()

除了重写delete 函数外,它并没有做太多的事情。此覆盖与本主题无关,但为了完整起见,我将其包括在内。它还将first_namelast_name 设置为None,它完美地工作并按预期从数据库中删除这些字段。我尝试将user 设置为None,但这无济于事。我也尝试将username 设置为None 但这会引发FieldNotFound 错误ACCOUNT_USER_MODEL_USERNAME_FIELD = None

我的设置(相关位):

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)
AUTH_USER_MODEL = 'custom_account.CustomUser'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'

我的迁移文件:

migrations.CreateModel(
            name='CustomUser',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('password', models.CharField(max_length=128, verbose_name='password')),
                ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
                ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
                ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
                ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
                ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
                ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
                ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
                ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
                ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
            ],
....

这一迁移世代让我无所适从。为什么username 字段仍然存在?为什么即使我已经明确设置了ACCOUNT_UNIQUE_EMAIL = True,它仍被设置为唯一的唯一约束?

注意:此迁移文件是从零开始生成的,这是从我提供的代码生成的第一个也是唯一一个迁移文件。

起初我以为,我的设置根本没有被读取。但是我检查了django.conf.settingsallauth.account.app_settings(在shell 中)是否有这些更改,它们都已更新。这是怎么回事?

注意:在我搜索过的许多 stackoverflow 问题中,this 问题似乎可以完美地解释我的问题。对于一个小问题,allauth 的创建者自己的回答建议使用ACCOUNT_USER_MODEL_USERNAME_FIELD = "username",因为有问题的模型是“显然使用了username 字段”。但是答案并没有说明当您根本不想使用username 字段时该怎么办。

【问题讨论】:

    标签: python django django-allauth


    【解决方案1】:

    看起来摆脱username 字段的唯一方法是覆盖AbstractUser 的用户名字段和/或从头开始使用完全自定义的模型。认为覆盖 AbstractBaseUser 也应该有效,尽管 AbstractBaseUser 提供的功能较少。

    class CustomUser(AbstractUser):
        # Custom user model for django-allauth
        # Remove unnecessary fields
        username = None
        first_name = None
        last_name = None
        # Set the email field to unique
        email = models.EmailField(_('email address'), unique=True)
        # Get rid of all references to the username field
        EMAIL_FIELD = 'email'
        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = []
    

    此模型将删除username 字段并使email 字段唯一,并将所有对USERNAME_FIELD 的引用更改为'email'。请注意REQUIRED_FIELDS 应该为空,因为USERNAME_FIELD 不能在其中。使用 allauth 时,这不是问题,电子邮件和密码要求无论如何都由 allauth 管理。

    我在问题中提到的设置应该保持不变,特别是-

    ACCOUNT_USER_MODEL_USERNAME_FIELD = None
    ACCOUNT_USERNAME_REQUIRED = False
    ACCOUNT_EMAIL_REQUIRED = True
    

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 2023-03-26
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2013-07-12
      • 2021-04-15
      • 2019-12-30
      • 2019-10-17
      相关资源
      最近更新 更多