Search code examples
djangodjango-admin

User password is not hashed when creating through Django admin panel


I have inherited the user class to make custom authentication. I do not get password hashing this way. It just stores as plain text in MySQL database. I have created staff through admin panel and unable to login as staff. Furthermore I have also created auth API endpoints using DRF and Djoser and am unable to login with the user profiles created through Django admin panel.

Here is my code.

models.py

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

admin.py

from .models import User

class UserAdmin(admin.ModelAdmin):
    pass
admin.site.register(User, UserAdmin)

I have seen old replies in Stack Overflow suggesting changing the parent class to django.contrib.auth.admin.UserAdmin . When I try this the add user template form only has 3 fields. Username, Password and Password Confirmation.

admin.py

from django.contrib.auth.admin import UserAdmin as DefaultUserAdmin
from .models import User

class UserAdmin(DefaultUserAdmin):
    pass

admin.site.register(User, UserAdmin)

How do I solve this problem.


Solution

  • I wrote a custom UserAdmin as well so i guess i can help you a little bit with that

    try this one:

    @admin.register(User)
    class UserAdmin(UserAdmin):
        """Define admin model for custom User model with no email field."""
    
        fieldsets = (
            (None, {'fields': ('email', 'password')}),
            (_('Personal info'), {'fields': ('first_name', 'last_name')}),
            (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser')}),
        )
        add_fieldsets = (
            (None, {
                'classes': ('wide',),
                'fields': ('email', 'password1', 'password2'),
            }),
        )
        list_display = ('email', 'is_staff')
        search_fields = ('email',)
        ordering = ('email',)
    

    And i also think that there`s problems in your models.py bcs you dont have userManager

    UserManager uses when you`re creating user so i guess problem in that