减少 Django 1.10 和 django-registration 2.2 中的用户名长度

Decrease username length in Django 1.10 and django-registration 2.2

如何减少用户名长度?
我正在使用 Django 1.10 和 django-registration 2.2。
我正在使用 "HMAC activation workflow" and the base form class "registration.forms.RegistrationFormUniqueEmail".
我是 Django 的新手,有点不知所措。
我必须编写一个完整的新自定义用户模型还是有一个简单的解决方案?

你需要做的是扩展 django-registration 的 form class and add custom field validation,像这样:

from django import forms

from registration.forms import RegistrationFormUniqueEmail


class RegistrationForm(RegistrationFormUniqueEmail):
    def clean_username():
        data = self.cleaned_data['username']
        if len(date) < 20:
            raise forms.ValidationError("Username has to be longer then 20 characters.")

        return data

然后您必须覆盖视图并使用上面的表单:

By default, this workflow uses registration.forms.RegistrationForm as its form class for user registration; this can be overridden by passing the keyword argument form_class to the registration view.