Starting with Django 1.9, you can determine how to define your own password checks. If you want, you can even just override the existing one. When you do, add a method:
from django.contrib.auth.password_validation import MinimumLengthValidator class MyPasswordValidator(MinimumLengthValidator): def password_changed(self, password, user):
Be sure to include your new class in your settings as follows:
AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'my_package.password_validators.MyPasswordValidator', 'OPTIONS': { 'min_length': 8, } }, ... ]
Now, every time the password is changed by the user, your MyPasswordValidator class will be notified. In my experience, this is the best way to do this because:
- When using signals to capture these events, you will also record events in which the system has re-encoded an existing password due to a change in the hashing parameters, in most cases you would not want to capture these events, and there is no obvious way to prevent it with signals.
- You can simply add a call function to the save () method of all forms of password processing, but this becomes difficult if you want to do the same with the built-in administrator password form, but you cannot, if password changes are made programmatically outside the form.
I warn you that you know that the password parameter in password_changed () is the user's raw password. Exercise caution when handling this and absolutely never store it anywhere unencrypted / unhashed.
source share