3

我试图集成 django-allauth,并尝试使用自定义用户模型。如此处所述,我创建了我的模型

class Client(AbstractBaseUser):
    fname = models.CharField(max_length=50)
    lname = models.CharField(max_length=50)
    email = models.EmailField(max_length=150, unique=True, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    state_choices = (
        (0, 'Pending'),
        (1, 'Active'),
        (2, 'Deleted'),
        (3, 'Banned'),
    )
    state = models.SmallIntegerField(choices=state_choices, default=1, editable=False)
    objects = ClientManager()
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['fname', 'lname']
    def save(self, *args, **kwargs):
        #########
    def get_short_name(self):
        return self.fname

    def get_full_name(self):
        return '%s %s' % (self.fname, self.lname)

    def has_perm(self, perm, obj=None):
        ###########

    def has_module_perms(self, app_label):
        ###########

    @property
    def is_staff(self):
        ###########

还有经理

class ClientManager(BaseUserManager):
    def create_user(self, fname, lname, email, created_at, state):
        user = self.model(fname=fname, lname=lname, email=email, created_at=created_at, state=state)
        return user

    def create_superuser(self, fname, lname, email, created_at, state):
        user = self.create_user(fname=fname, lname=lname, email=email, created_at=created_at, state=state)
        user.is_team_player = True
        user.save()
        return user

现在,对于admin.py我所做的

class UserCreationForm(forms.ModelForm):
    #A form for creating new users
    class Meta:
        model = models.Client
        fields = ('fname', 'lname', 'email')


class UserChangeForm(forms.ModelForm):
    #A form for updating users
    class Meta:
        model = models.Client
        fields = ['fname', 'lname', 'email', 'is_admin']

class ClientAdmin(UserAdmin):
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    list_display = ('email', 'is_admin')
    list_filter = ('is_admin',)

    fieldsets = (
    (None, {'fields': ('email', )}),
    ('Personal info', {'fields': ('fname', 'lname')}),
    ('Permissions', {'fields': ('is_admin',)}),
    )  

    add_fieldsets = (
    (None, {
        'classes': ('wide',),
        'fields': ('email', 'fname', 'lname')}
    ),
)

    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()




admin.site.unregister(models.Client)
admin.site.register(models.Client, ClientAdmin)
admin.site.unregister(Group)

settings.py我添加AUTH_USER_MODEL = 'app.Client'

现在当我去/admin它给我的错误

ImproperlyConfigured at /admin
Cannot resolve keyword 'username' into field. Choices are: activityhistory, app, created_at, email, emailaddress, fname, id, is_active, is_admin, last_login, lname, logentry, password, socialaccount, state, testdevice

堆栈跟踪告诉我,因为它被视为email用户名,并且我使用usernameandpassword登录,所以它给了我这个错误。我不能让 django 忽略这个仅限于管理员的限制,还是我可以将管理员的登录机制更改为使用电子邮件而不是用户名?

我试图django-allauth让用户通过谷歌登录或使用他的电子邮件和密码注册。但到目前为止,这对我来说是一段艰难的旅程。如果有人以前做过或知道如何轻松做到这一点,那么向我指出参考资料会很棒。

4

1 回答 1

2

首先,将其添加到您的自定义用户模型中:

class Client(AbstractBaseUser):
   class Meta:
      swappable = 'AUTH_USER_MODEL'

此外,将其添加到您的 settings.py 文件中:

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None

从文档:

(...) 如果您的自定义用户模型没有用户名字段(同样,不要误认为是电子邮件地址或用户 ID),您需要将 ACCOUNT_USER_MODEL_USERNAME_FIELD 设置为 None。这将禁用 allauth 中与用户名相关的功能。

这对我有用。

参考:

[1] https://github.com/pennersr/django-allauth#custom-user-models

[2] https://code.djangoproject.com/wiki/ContribAuthImprovements

于 2013-12-31T00:58:44.773 回答