Django admin change user password

2019-08-03 15:53发布

问题:

I am new to Django. I have the following code:

Model:

class MyUser(AbstractUser):
    profile = models.OneToOneField(Profile, null=True, on_delete=models.PROTECT)

My profile model contains only some personal fields like full_name, gender, birthdate, etc.

Admin:

class MyUserInline(admin.StackedInline):
    model = MyUser
    exclude = ('first_name', 'last_name', 'username', 'plan', 'password')
    fieldsets = (
        (_('Personal info'), {'fields': ('email',)}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )


class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = MyUser

    def clean_password(self):
        return ""

class ProfileAdmin(admin.ModelAdmin):
    inlines = (MyUserInline,)
    change_user_password_template = None
    form = MyUserChangeForm
    change_password_form = AdminPasswordChangeForm
    ordering = ('myuser__email',)

    def save_model(self, request, obj, form, change):
        obj.myuser.username = obj.myuser.email
        obj.save()

    list_display = ('full_name', 'myuser_email', 'myuser_is_staff', 'myuser_is_superuser', 'myuser_is_active')

    def user_change_password(self, request, id, form_url=''):
    if not self.has_change_permission(request):
        raise PermissionDenied
    user = self.get_object(request, unquote(id))
    if user is None:
        raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {
            'name': force_text(self.model._meta.verbose_name),
            'key': escape(id),
        })
    if request.method == 'POST':
        form = self.change_password_form(user, request.POST)
        if form.is_valid():
            form.save()
            # rest of method

    # other minor methods 

I cannot change the user's password. At first the change password page doesn't open at all (Profile with ID "3/password" doesn't exist. Perhaps it was deleted?)

After adding the following code it opens, but after pressing save button, I get 'Profile' object has no attribute 'set_password'

def get_urls(self):
    return [
               url(
                   r'^(.+)/password/$',
                   self.admin_site.admin_view(self.user_change_password),
                   name='auth_user_password_change',
               ),
           ] + super(ProfileAdmin, self).

After adding following lines, no error is arised, and after hitting the save button it should say changed successfully, but it hasn't.

class Profile(models.Model):
.......

    def set_password(self, raw_password):
        self.myuser.password = make_password(raw_password)
        self.myuser._password = raw_password

回答1:

The problem was solved by :

class MyAdminPasswordChangeForm(AdminPasswordChangeForm):

    def save(self, commit=True):
        """
        Saves the new password.
        """
        password = self.cleaned_data["password1"]
        self.user.myuser.set_password(password)
        if commit:
            self.user.myuser.save()
        return self.user.myuser

and

class ProfileAdmin(UserAdmin):
    change_password_form = MyAdminPasswordChangeForm