仅使用电子邮件和密码创建Django用户 - UserCreationForm

时间:2017-07-13 13:22:35

标签: python django django-users

我需要仅使用emailpassword字段在我的应用中创建用户帐户。因此,我在models.py中的自定义用户模型是:

我自定义UserManager以创建用户

from django.contrib.auth.models import BaseUserManager

class UserManager(BaseUserManager):
    def _create_user(self, email, password, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError("Users must have an email address")
            email = self.normalize_email(email)
            user = self.model(email = email, **extra_fields)
            user.set_password(password)
            user.save()
            return user

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self._create_user(email, password, **extra_fields)

我的用户模型是:

from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils.translation import ugettext_lazy as _

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True, null=True,
            help_text=_('Required. Letters, digits and ''@/./+/-/_ only.'),
        validators=[RegexValidator(r'^[\w.@+-]+$', _('Enter a valid email address.'), 'invalid')
        ])

    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this site.'),
    )

    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )

    objects = UserManager()
    USERNAME_FIELD = "email"

    class Meta:
        db_table = 'auth_user'
        verbose_name_plural = 'Usuarios en la plataforma'

    def __str__(self):
        return "@{}".format(self.email)

在我的设置中,我添加了:

AUTH_USER_MODEL = ‘my_app_name.User’

创建用户 - UserCreationForm预建类

要创建用户,我使用的是UserCreationFormpre-built in the django core.

在此课程中,用户名字段使用such as denoted here

根据以上所述,在我的forms.py中我有:

from django.contrib.auth.forms import UserChangeForm, UserCreationForm

class CustomUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = get_user_model()

class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = get_user_model()

class UserCreateForm(UserCreationForm):

    class Meta:
        fields = ("email", "password1", "password2",)
        model = get_user_model()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["email"].label = "Email address"

当我尝试执行python manage.py makemigrations时,我得到了这个回溯输出错误

    bgarcial@elpug ‹ testing ●● › : ~/workspace/ihost_project
[1] % python manage.py makemigrations accounts 
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/bgarcial/workspace/ihost_project/accounts/admin.py", line 8, in <module>
    from .forms import CustomUserChangeForm, CustomUserCreationForm
  File "/home/bgarcial/workspace/ihost_project/accounts/forms.py", line 16, in <module>
    class CustomUserCreationForm(UserCreationForm):
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/forms/models.py", line 257, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (username) specified for User
(ihost) 
bgarcial@elpug ‹ testing ●● › : ~/workspace/ihost_project

当然,我正在使用UserCreationForm django类核心,我强制使用需要用户名字段的django核心功能

如何删除用户名或修改此用户名?

我知道不建议修改django核心,但是,如何在不包含使用UserCreationForm django类核心的用户名字段的情况下创建用户?

我尝试在我创建用户的地方覆盖我的表单的save方法,但是我没有清楚这个过程,我认为我不方便的核心是使用UserCreationForm django类核心..

class UserCreateForm(UserCreationForm):
    class Meta:
        fields = ("email", "password1", "password2",)
        model = get_user_model()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["email"].label = "Email address"

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]

        # Tell to Django that not check the username

        if commit:
            user.save()
        return user

如果有人能指出我正确的方向将非常感激。 :)

1 个答案:

答案 0 :(得分:2)

我找到了一个有效的解决方案。

无论如何,随时建议更好的解决方案!

就像我的不便/错误与使用pre-built in the django coreUserCreationFormuse the username field in their logic有关,我接着进行了以下操作:

我班级CustomUserCreationForm中的forms.py是班级UserCreationForm的孩子,我已覆盖/添加Meta类属性fields ,使用email字段而不是username字段。 This question post帮助我。

我的班级CustomUserCreationForm保持如下:

class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = get_user_model()
        fields = ('email',)

然后,我继续执行迁移:

[1] % python manage.py makemigrations accounts 
SystemCheckError: System check identified some issues:

ERRORS:
<class 'accounts.admin.UserAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'username', which is not an attribute of 'accounts.User'.

此错误向我显示username字段不是我的User模型的属性。 这意味着Django会尝试询问用户名字段,即使我用fields字段覆盖了email值。

当然这是逻辑,因为我仍然继承自UserCreationFormpre-built in the django core

然后,我已使用null = True属性将username字段添加到我的用户模型中,这样,在用户帐户创建中不需要用户名:

class User(AbstractBaseUser, PermissionsMixin):

    # I had add the username field despite that I don't use in my User model
    username = models.CharField(_('username'), max_length=30, null=True,
            help_text=_('Required. 30 characters or fewer. Letters, digits and ''@/./+/-/_ only.'),
        validators=[RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid')
        ])

    email = models.EmailField(unique=True, null=True,
            help_text=_('Required. Letters, digits and ''@/./+/-/_ only.'),
        validators=[RegexValidator(r'^[\w.@+-]+$', _('Enter a valid email address.'), 'invalid')
        ])

  ...

通过这种方式,我执行我的迁移

bgarcial@elpug ‹ testing ●● › : ~/workspace/ihost_project
[1] % python manage.py makemigrations accounts 
Migrations for 'accounts':
  accounts/migrations/0001_initial.py:
    - Create model User
(ihost) 
bgarcial@elpug ‹ testing ●● › : ~/workspace/ihost_project

python manage.py migrate accounts ...

我的用户名字段仍然存在于我的自定义用户架构中,这不是必需的,当我从UserCreateForm类创建一个继承自UserCreationForm的用户时,我可以创建一个用户帐户只是电子邮件和密码

enter image description here

我不知道这是否是解决这个问题的最佳方法。 随意提出改进建议!