ubernostrum / django-registration

An extensible user-registration app for Django.
BSD 3-Clause "New" or "Revised" License
923 stars 241 forks source link

Adding a User from admin using the custom user model throws KeyError for 'email' #186

Closed realnitinworks closed 4 years ago

realnitinworks commented 5 years ago

I have a custom user model and integrated the django_registration 2-step activation work flow into the app. The resgitration process is working fine from the app's signup form which I created.

However I am not able to add a User from the admin anymore. When I try to do that I get the following error:


Request Method: | GET
-- | --
http://127.0.0.1:8000/admin/accounts/customuser/add/
2.2
KeyError
'email'
/home/nitin/.local/share/virtualenvs/bookmark-GvaJsEPx/lib/python3.7/site-packages/django_registration/forms.py in __init__, line 59
/home/nitin/.local/share/virtualenvs/bookmark-GvaJsEPx/bin/python
3.7.3
['/home/nitin/Study/Django/myapps/bookmark',  '/home/nitin/.local/share/virtualenvs/bookmark-GvaJsEPx/lib/python37.zip',  '/home/nitin/.local/share/virtualenvs/bookmark-GvaJsEPx/lib/python3.7',  '/home/nitin/.local/share/virtualenvs/bookmark-GvaJsEPx/lib/python3.7/lib-dynload',  '/home/nitin/anaconda3/lib/python3.7',  '/home/nitin/.local/share/virtualenvs/bookmark-GvaJsEPx/lib/python3.7/site-packages']

Following are my relevant code:

#admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from accounts.forms import CustomUserCreationForm, CustomUserChangeForm
from accounts.models import CustomUser

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['username', 'email', 'age', 'company', 'is_staff']

admin.site.register(CustomUser, CustomUserAdmin)
# forms.py
from django_registration.forms import RegistrationForm

from accounts.models import CustomUser

class CustomUserCreationForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
        fields = ('username', 'email', 'age', 'company',)

class CustomUserChangeForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
        fields = ('username', 'email', 'age', 'company',)
#models.py
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    age = models.PositiveIntegerField(null=True, blank=True)
    company = models.CharField(max_length=250, blank=True)
ubernostrum commented 4 years ago

This seems to be something wrong with your admin config, not with django-registration. Please try a general Django support channel for help with configuring the Django admin.