Changing password in Django Admin

前端 未结 7 1712
一个人的身影
一个人的身影 2020-12-14 19:49

I recently created the admin.py based in the Django Project Document:

https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models

7条回答
  •  离开以前
    2020-12-14 20:12

    I added this method to my UserAdmin class:

    def save_model(self, request, obj, form, change):
        # Override this to set the password to the value in the field if it's
        # changed.
        if obj.pk:
            orig_obj = models.User.objects.get(pk=obj.pk)
            if obj.password != orig_obj.password:
                obj.set_password(obj.password)
        else:
            obj.set_password(obj.password)
        obj.save()
    

    You can the show the password field normally, but admins will only see the hashed password. If they alter it, the new value is then hashed and save.

    This adds a single query to each time you save a user via the admin. It should generally not be an issue, since most systems don't have admins intensively editing users.

提交回复
热议问题