为django 1.5定制用户模型,子类化django-registration 1.0表单。

时间:2021-10-30 19:21:16

django-registration 1.0 now has support for django 1.5 custom user models. The django-registration documentation only has the following FAQ item about it:

django-registration 1.0现在支持django 1.5自定义用户模型。django注册文件中只有以下常见问题:

I’m using Django 1.5 and a custom user model; how do I make that work?

我使用的是Django 1.5和一个自定义用户模型;我怎么做呢?

Although the two built-in backends supplied with django-registration both assume Django’s default User model, the base view classes are deliberately user-model-agnostic. Simply subclass them, and implement logic for your custom user model.

虽然Django -registration提供的两个内置后端都假定Django的默认用户模型,但是基本视图类是故意与用户模型无关的。只需对它们进行子类化,并为自定义用户模型实现逻辑。

I'm not sure which views I need to subclass and what should go in them. I've also noticed that the ProfileManager in django-registration still assumes a separate username field.

我不确定我需要对哪些视图进行子类化,以及它们应该包含哪些内容。我还注意到,django注册中的ProfileManager仍然假设有一个单独的用户名字段。

In my specific case, I've removed the 'username' field, added a 'display_name', and made 'email' the identifying field:

在我的具体案例中,我删除了“username”字段,添加了“display_name”字段,并将“email”字段设置为标识字段:

class MyUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name="Email Address",
        max_length=384,
        unique=True,
        db_index=True,)
    display_name = models.CharField(max_length=128, blank=True)
    date_joined = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

Without subclassing any django-registration classes, the default rendering of the registration form pulls in fields from User instead of MyUser.

没有子类化任何django注册类,注册表单的默认呈现将从User而不是MyUser获取字段。

I've seen the following SO thread django-registration app and Django 1.5 custom user model, but it didn't help.

我已经看到了下面的线程Django -registration应用程序和Django 1.5定制用户模型,但它并没有帮助。

Update

更新

I've noticed that RegistrationForm is hardcoded with a 'username' field. The FAQ only mentions subclassing the backend, so I'm not sure what the intention is here. Should I subclass the form as well?

我注意到RegistrationForm是用“username”字段硬编码的。FAQ只提到对后端进行子类化,因此我不确定这里的目的是什么。我也应该把表单子类化吗?

1 个解决方案

#1


1  

Some parts are most definitely not Django 1.5 compatible yet: https://bitbucket.org/ubernostrum/django-registration/src/8f242e35ef7c004e035e54b4bb093c32bf77c29f/registration/forms.py?at=default#cl-48

有些部件肯定还不兼容Django 1.5: https://bitbucket.org/ubernostrum/django- registration/src/8f242e35ef7c004e4e035e4b4bb093c32bf77f/registration/forms.pyatdefault #cl-48

class RegistrationForm(forms.Form):
    # ...

    def clean_username(self):
        # ...
        # The line below needs fixing
        existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
        if existing.exists():
            raise forms.ValidationError(_("A user with that username already exists."))
        else:
            return self.cleaned_data['username']

So unless those methods are changed and/or you subclass them, it won't work yet.

所以,除非这些方法被修改和/或您对它们进行子类化,否则它还不会工作。

For your specific case this registration form should do the trick:

对于您的具体情况,这张登记表应该能起到以下作用:

from registration import forms as registration_forms
from django.contrib import auth

class RegistrationForm(registration_forms.RegistrationForm):
    def clean_username(self):
        '''
        Validate that the username is alphanumeric and is not already
        in use.
        '''
        User = auth.get_user_model()
        existing = User.objects.filter(display_name__iexact=self.cleaned_data['username'])
        if existing.exists():
            raise forms.ValidationError(_("A user with that name already exists."))
        else:
            return self.cleaned_data['username']

In addition to a custom property on your model:

除了您的模型上的自定义属性之外:

class MyUser(AbstractBaseUser, PermissionsMixin):
    # ...

    def get_username(self):
        return self.display_name

    def set_username(self, username):
        self.display_name = username

    def del_username(self):
        del self.display_name

    username = property(get_username, set_username, del_username)

#1


1  

Some parts are most definitely not Django 1.5 compatible yet: https://bitbucket.org/ubernostrum/django-registration/src/8f242e35ef7c004e035e54b4bb093c32bf77c29f/registration/forms.py?at=default#cl-48

有些部件肯定还不兼容Django 1.5: https://bitbucket.org/ubernostrum/django- registration/src/8f242e35ef7c004e4e035e4b4bb093c32bf77f/registration/forms.pyatdefault #cl-48

class RegistrationForm(forms.Form):
    # ...

    def clean_username(self):
        # ...
        # The line below needs fixing
        existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
        if existing.exists():
            raise forms.ValidationError(_("A user with that username already exists."))
        else:
            return self.cleaned_data['username']

So unless those methods are changed and/or you subclass them, it won't work yet.

所以,除非这些方法被修改和/或您对它们进行子类化,否则它还不会工作。

For your specific case this registration form should do the trick:

对于您的具体情况,这张登记表应该能起到以下作用:

from registration import forms as registration_forms
from django.contrib import auth

class RegistrationForm(registration_forms.RegistrationForm):
    def clean_username(self):
        '''
        Validate that the username is alphanumeric and is not already
        in use.
        '''
        User = auth.get_user_model()
        existing = User.objects.filter(display_name__iexact=self.cleaned_data['username'])
        if existing.exists():
            raise forms.ValidationError(_("A user with that name already exists."))
        else:
            return self.cleaned_data['username']

In addition to a custom property on your model:

除了您的模型上的自定义属性之外:

class MyUser(AbstractBaseUser, PermissionsMixin):
    # ...

    def get_username(self):
        return self.display_name

    def set_username(self, username):
        self.display_name = username

    def del_username(self):
        del self.display_name

    username = property(get_username, set_username, del_username)