liveBook · Manning

Validator

As explained in Django in Action

Code that checks whether a value satisfies specified criteria and raises a validation error when it does not.

A validator is code that checks whether a value meets specified criteria. When the value fails, the validator raises a ValidationError. Django supplies built-in validators and lets you attach them to fields or write your own. section 14.1

Password validation is configured through AUTH_PASSWORD_VALIDATORS, a list of validator settings. Django’s defaults check username similarity, minimum length, common passwords, and whether the password contains a number. section 9.1.2

A validator can be used in several practical ways:

  • Use Django’s built-in validators for common rules such as password length and common-password detection. section 9.1.2
  • Attach a specific validator to a model field; some model fields are character fields with a validator already attached. section 14.1
  • Write a custom validator and register it in AUTH_PASSWORD_VALIDATORS when the built-in rules are not enough. section 9.1.2

Changing a password from the command line

Run `python manage.py changepassword nicky`, enter the new password twice, and Django reports success only after applying the current password-security rules. Unlike createsuperuser, changepassword does not offer a way to bypass password validation. section 9.1.2

(venv) RiffMates$ python manage.py changepassword nicky
Changing password for user 'nicky'
Password:
Password (again):
Password changed successfully for user 'nicky'

The command transcript shows the input-and-confirmation flow and the resulting success message. section 9.1.2

How Django in Action covers it

How deep it goes
Defines validation, names built-in checks, and shows customization, but defers implementation details to documentation.
Examples
Uses password-strength checks and the changepassword management command as concrete examples.
What you can do after
Operate/configure password validation with changepassword and AUTH_PASSWORD_VALIDATORS, or register a custom validator.
How it connects
Connects validators with ValidationError, model fields, and AUTH_PASSWORD_VALIDATORS.

Also explained in

Compare how 2 books explain validator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.