Created
October 19, 2020 10:06
-
-
Save AshishPandagre/ecb68e2967503c56fcfbd73c03d88917 to your computer and use it in GitHub Desktop.
creating a custom user model in django-allauth.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------some_app/admin.py------------- | |
from django.contrib import admin | |
from django.contrib.auth.admin import UserAdmin | |
from .models import User | |
class CustomUserAdmin(UserAdmin): | |
fieldsets = ( | |
*UserAdmin.fieldsets, # original form fieldsets, expanded | |
( # new fieldset added on to the bottom | |
'Custom Field Heading', # group heading of your choice; set to None for a blank space instead of a header | |
{ | |
'fields': ( | |
'custom_field1', | |
'custom_field2' | |
), | |
}, | |
), | |
) | |
admin.site.register(User, CustomUserAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------some_project/forms.py------------- | |
# it displays what fields will be asked during signup, login..etc..etc. | |
from allauth.account.forms import SignupForm | |
from django import forms | |
class CustomSignupForm(SignupForm): | |
first_name = forms.CharField(label='First Name') | |
last_name = forms.CharField(label='Last Name') | |
custom_field1 = forms.IntegerField(label='Custom field 1') | |
custom_field2 = forms.CharField(label='Custom field 2') | |
def save(self, request): | |
user = super(CustomSignupForm, self).save(request) | |
custom_field1 = self.cleaned_data['custom_field1'] | |
custom_field2 = self.cleaned_data['custom_field2'] | |
user.custom_field1 = custom_field1 | |
user.custom_field2 = custom_field2 | |
user.save() | |
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------some_app/models.py------------- | |
from django.db import models | |
from django.contrib.auth.models import AbstractUser | |
class User(AbstractUser): | |
custom_field1 = models.IntegerField(default=10) | |
custom_field2 = models.CharField(max_length=20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------some_project/settings.py------------- | |
# make sure django-allauth settings are properly implemented . | |
# custom user model | |
AUTH_USER_MODEL = 'some_app.User' | |
# custom signup page | |
ACCOUNT_FORMS = { | |
'signup': 'some_project.forms.CustomSignupForm', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment