我试图整合django-allauth,我尝试使用自定义用户模型。如上所述here,我创建了我的模型
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
和password
进行登录,因此会出现此错误。我不能让django忽略管理员的这个限制,或者我可以更改管理员使用电子邮件而不是用户名的登录机制吗?
我正在尝试使用django-allauth
让用户通过谷歌或注册使用他的电子邮件和密码登录。但到现在为止,这对我来说是艰难的。如果有人之前已经这样做过或者知道如何轻松地做到这一点,那么指点我的参考文章就会很棒。
答案 0 :(得分: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设置为无。这将禁用 allauth中与用户名相关的功能。
这对我有用。
参考文献:
[1] https://github.com/pennersr/django-allauth#custom-user-models
[2] https://code.djangoproject.com/wiki/ContribAuthImprovements