Changing password in Django Admin

前端 未结 7 1714
一个人的身影
一个人的身影 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:24

    You can also do like this, in this way you just have to write over the field password and once you will save it, it will create the hash for it :

    class UserModelAdmin(admin.ModelAdmin):
    
        """
            User for overriding the normal user admin panel, and add the extra fields added to the user
            """
    
    
    def save_model(self, request, obj, form, change):
        user_database = User.objects.get(pk=obj.pk)
        # Check firs the case in which the password is not encoded, then check in the case that the password is encode
        if not (check_password(form.data['password'], user_database.password) or user_database.password == form.data['password']):
            obj.password = make_password(obj.password)
        else:
            obj.password = user_database.password
        super().save_model(request, obj, form, change)
    
    0 讨论(0)
提交回复
热议问题