Reputation: 5411
I was trying to integrate django-allauth, and I tried to use custom user model. As stated here, I created my model
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):
###########
And the manager
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
Now, for the admin.py
I did
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)
In the settings.py
I added AUTH_USER_MODEL = 'app.Client'
Now when I go to /admin
it gives me the error
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
The stack trace tells me that since it treats email
as username, and I use username
and password
for logging in, it gives me this error. Can't I make django ignore this restriction for admin only, or can I change the login mechanism for admin to use email rather than username?
I am trying to use django-allauth
for letting a user sign-in through google or signup using his email and password. But its been a tough ride for me till now. If anybody has done that before or knows how to do it easily, pointing me to references would be great.
Upvotes: 3
Views: 1502
Reputation: 2678
First, add this to your custom user model:
class Client(AbstractBaseUser):
class Meta:
swappable = 'AUTH_USER_MODEL'
Also, add this to your settings.py file:
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
From the docs:
(...) if your custom user model does not have a username field (again, not to be mistaken with an e-mail address or user id), you will need to set ACCOUNT_USER_MODEL_USERNAME_FIELD to None. This will disable username related functionality in allauth.
That worked for me.
References:
[1] https://github.com/pennersr/django-allauth#custom-user-models
[2] https://code.djangoproject.com/wiki/ContribAuthImprovements
Upvotes: 2