我试图集成 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
用户名,并且我使用username
andpassword
登录,所以它给了我这个错误。我不能让 django 忽略这个仅限于管理员的限制,还是我可以将管理员的登录机制更改为使用电子邮件而不是用户名?
我试图django-allauth
让用户通过谷歌登录或使用他的电子邮件和密码注册。但到目前为止,这对我来说是一段艰难的旅程。如果有人以前做过或知道如何轻松做到这一点,那么向我指出参考资料会很棒。