'Django admin error "Please correct the errors below."
I have made custom BaseUserManager to create user but I am getting error of "please correct the errors below" when I add user from django admin panel and I can't find what's going wrong.
Models.py:
class UserManager(BaseUserManager):
def create_user(self,email,password):
user = self.model(email=email)
user.set_password(password)
def create_superuser(self,email,password):
user = self.model(email=email)
user.set_password(password)
user.is_admin = True
user.is_superuser = True
user.is_staff=True
user.save(using=self._db)
class User(AbstractBaseUser,PermissionsMixin):
COMPANY='CO'
EMPLOYEE='EM'
STATUS_CHOICES=(
(COMPANY,'Company'),
(EMPLOYEE,'Employee'),
)
Status=models.CharField(max_length=2,
choices=STATUS_CHOICES,
default=EMPLOYEE)
user_image = models.CharField(max_length=100)
is_admin=models.BooleanField()
email=models.EmailField(unique=True)
is_staff=models.BooleanField(default=False)
object = UserManager()
USERNAME_FIELD='email'
REQUIRED_FIELDS = []
objects=models.Manager()
def get_short_name(self):
return self.email
Admin.py files:
class UserAdmin(UserAdmin):
list_display = ('email', 'is_admin')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('email', 'password','Status','user_image','last_login')}),
('Permissions', {'fields': ('is_admin','is_staff','is_superuser','user_permissions','groups')}),
)
add_fieldsets= (
(None, {'fields': ('email', 'password','Status','user_image','last_login')}),
('Permissions', {'fields': ('is_admin','is_staff','is_superuser','user_permissions','groups')}),
)
search_fields = ('password',)
ordering = ('password',)
filter_horizontal = ()
admin.site.register(User,UserAdmin)
admin.site.register(Company)
admin.site.register(Employee)
admin.site.register(Job)
admin.site.register(AppliedJobs)
Can someone suggest what I am doing wrong? I always get the error when I add user from admin panel.I can't figure out as I am working first time on baseusermanager.
Solution 1:[1]
I can suggest how to try and find the problem. Usually, your UserAdmin will refer to forms, see below:
class UserAdmin(UserAdmin):
add_form = SignUpForm
form = CustomUserChangeForm
You can find out what is causing the errors by using logging. Add the following Mixin to your SignUpForm and CustomUserChangeForm:
import logging
logger = logging.getLogger(__name__)
# see https://stackoverflow.com/questions/20833638/how-to-log-all-django-form-validation-errors
class LoggingMixin(object):
def add_error(self, field, error):
if field:
logger.info('Form error on field %s: %s', field, error)
else:
logger.info('Form error: %s', error)
super().add_error(field, error)
# e.g.
class SignUpForm(LoggingMixin, UserCreationForm):
....
Now, either sign in or change a user and check the logs to find out what is causing the "Please correct the errors below.".
Solution 2:[2]
Had the same problem and answer is pretty simple.
The problem is in your add_fieldsets. Django use his own pattern for adding new users (you can check it in UserAdmin model in django.contrib.auth.admin.py file). You need to define fields for password1 and password2.
For your convenience I will paste it there:
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2'),
}),
)
Here you have 2 options:
P.S. in any case you need to define username, password1 and password2 fields.
- Use superclass field and add your custom:
add_fieldsets = UserAdmin.add_fieldsets + (
(None, {'fields': ('email', 'Status', 'user_image', 'last_login')}),
('Permissions', {'fields': ('is_admin', 'is_staff', 'is_superuser', 'user_permissions', 'groups')}),
)
- Define by your own:
add_fieldsets = (
(None, {'fields': ('username', 'email', 'password1', 'password2', 'Status', 'user_image', 'last_login')}),
('Permissions', {'fields': ('is_admin', 'is_staff', 'is_superuser', 'user_permissions', 'groups')}),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
