Custom user model in django does not allow setting password in admin
Asked Answered
N

4

6

I have created a custom user model which I am successfully using within my app.

The problem is that within the Admin, on the user edit screen, I get a display of the present password hash, instead of the very useful interface for setting the password.

I am using Django 1.5b1 on Python 2.7.

How can I convince the Django admin to treat my user model the same way it treats the native User, for the sake of the admin user interface?

Ney answered 1/12, 2012 at 17:33 Comment(0)
J
11

Documentation suggest that you need to register the custom model with admin and also define few methods as well so that admin interface works with custom user model.

You may also have to define built-in forms for User.

From Custom users and django.contrib.admin

You will also need to register your custom User model with the admin. If your custom User model extends AbstractUser, you can use Django's existing UserAdmin class. However, if your User model extends AbstractBaseUser, you'll need to define a custom ModelAdmin class.

Juli answered 1/12, 2012 at 17:45 Comment(1)
It worked! More details: my model is not based on UserAdmin, but looks, talks, an walks like one (has the same fields, and more). I was able to register my own custom model admin which I created with minimal effort and code duplication. My model admin extends django.contrib.auth.admin.UserAdmin and overrides the fieldsets, form, and add_form. form and add_form in turn, point to custom forms of mine which extends the Django-provided UserCreationForm and UserChangeForm and override the Meta subclass to point to my model. It's amazing how this stuff works together! Also, props to duck-typing.Ney
A
2

Just add this to your form:

  password = ReadOnlyPasswordHashField(
      label= ("Password"),
      help_text= ("Raw passwords are not stored, so there is no way to see "
                  "this user's password, but you can change the password "
                  "using <a href=\"password/\">this form</a>."))

Borrowed from https://mcmap.net/q/627278/-changing-password-in-django-admin

Alina answered 17/12, 2014 at 19:13 Comment(0)
R
1

At least in 1.10, I was able to do the following. Simpler than the above. I put it in admin.py:

from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from myapp.models import MyUser

class MyUserAdmin(UserAdmin):
    model = MyUser

    fieldsets = UserAdmin.fieldsets + (
            (None, {'fields': ('a_custom_field',)}),
    )

admin.site.register(MyUser, MyUserAdmin)
Rodrigues answered 29/1, 2017 at 8:18 Comment(0)
A
0

If you're extending AbstractBaseUser, you'll need two forms, a creation form, and an update form without the passwords, derived from ModelForm.

Then in the Admin form, specify separate create and update forms, along with corresponding fieldsets:

class MyUserAdmin(UserAdmin):
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    fieldsets = (
        (None, {'fields': ('email', 'first_name', 'last_name')}),
    )
    add_fieldsets = (
        (None, {'fields': ('email', 'password', 'password2', 'first_name', 'last_name')}),
    )

Passwords for new users will work. Do password changes in the pre-defined django form instead of in admin.

Alpha answered 25/7, 2013 at 2:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.