Fixed #21379 -- Created auth-specific username validators

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2016-04-22 19:39:13 +02:00
parent 2265ff3710
commit 526575c641
11 changed files with 168 additions and 33 deletions

View file

@ -32,6 +32,15 @@ Fields
``max_length=191`` because MySQL can only create unique indexes with
191 characters in that case by default.
.. admonition:: Usernames and Unicode
Django originally accepted only ASCII letters in usernames.
Although it wasn't a deliberate choice, Unicode characters have
always been accepted when using Python 3. Django 1.10 officially
added Unicode support in usernames, keeping the ASCII-only behavior
on Python 2, with the option to customize the behavior using
:attr:`.User.username_validator`.
.. versionchanged:: 1.10
The ``max_length`` increased from 30 to 150 characters.
@ -146,6 +155,27 @@ Attributes
In older versions, this was a method. Backwards-compatibility
support for using it as a method will be removed in Django 2.0.
.. attribute:: username_validator
.. versionadded:: 1.10
Points to a validator instance used to validate usernames. Defaults to
:class:`validators.UnicodeUsernameValidator` on Python 3 and
:class:`validators.ASCIIUsernameValidator` on Python 2.
To change the default username validator, you can subclass the ``User``
model and set this attribute to a different validator instance. For
example, to use ASCII usernames on Python 3::
from django.contrib.auth.models import User
from django.contrib.auth.validators import ASCIIUsernameValidator
class CustomUser(User):
username_validator = ASCIIUsernameValidator()
class Meta:
proxy = True # If no new field is added.
Methods
-------
@ -285,7 +315,6 @@ Manager methods
Same as :meth:`create_user`, but sets :attr:`~models.User.is_staff` and
:attr:`~models.User.is_superuser` to ``True``.
``AnonymousUser`` object
========================
@ -378,6 +407,25 @@ Fields
group.permissions.remove(permission, permission, ...)
group.permissions.clear()
Validators
==========
.. class:: validators.ASCIIUsernameValidator
.. versionadded:: 1.10
A field validator allowing only ASCII letters, in addition to ``@``, ``.``,
``+``, ``-``, and ``_``. The default validator for ``User.username`` on
Python 2.
.. class:: validators.UnicodeUsernameValidator
.. versionadded:: 1.10
A field validator allowing Unicode letters, in addition to ``@``, ``.``,
``+``, ``-``, and ``_``. The default validator for ``User.username`` on
Python 3.
.. _topics-auth-signals:
Login and logout signals