Hey everyone, I created a custom User model and admin.py. I'm having as
issue in the admin console where if I click save on a user, even without
modifying any fields, the user history always adds a 'Changed password.'
record. It looks like this used to be a problem (
https://github.com/django/django/pull/464 &
https://code.djangoproject.com/ticket/18460) but it also looks like it was
fixed. Can someone please help me to see if there's anything below that
would be causing this problem? If I click save when making no changes on a
Group, the correct 'No changes made.' message appears in the log, so it
looks like this is only happening with my custom User model. Thank you.
# appname/admin.py
from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from .models import UPDUser
class UPDUserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all of the required
fields, plus a repeated password.
"""
password1 = forms.CharField(label='Password',
widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm password',
widget=forms.PasswordInput)
class Meta:
model = UPDUser
fields = ('email', 'first_name', 'last_name')
def clean_password2(self):
"""Check that the two password entries match."""
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError("These passwords don't match.")
return password2
def save(self, commit=True):
"""Save the provided password in hashed format."""
user = super(UPDUserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
class UPDUserChangeForm(forms.ModelForm):
"""A form for updating users. Includes all of the fields on the
user, but replaces the password field with the admin's password
hash display field.
"""
password = ReadOnlyPasswordHashField()
class Meta:
model = UPDUser
def clean_password(self):
"""Return the initial password value."""
# Regardless of what the user provides, return the initial value.
# This is done here rather than on the field because the field
# does not have access to the initial value.
return self.initial['password']
class UPDUserAdmin(UserAdmin):
"""Set the add/modify forms."""
form = UPDUserChangeForm
add_form = UPDUserCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('email', 'first_name', 'last_name', 'is_staff')
list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email',)
filter_horizontal = ('groups', 'user_permissions')
fieldsets = (
(None, {'fields': ('first_name', 'last_name', 'email',
'password')}),
('Permissions', {'fields': ('is_active', 'is_staff',
'is_superuser', 'groups',
'user_permissions')}),
('Important dates', {'fields': ('last_login',)}))
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {'classes': ('wide',),
'fields': ('first_name', 'last_name', 'email',
'password1', 'password2')}),)
# Register the new UPDUserAdmin
admin.site.register(UPDUser, UPDUserAdmin)
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/e9f5e91b-67b9-45e1-bbbc-827c8104a00c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.