Search code examples
djangodjango-registration

Decrease username length in Django 1.10 and django-registration 2.2


how can I decrease the username length?
I am using Django 1.10 and django-registration 2.2.
I am using the "HMAC activation workflow" and the base form class "registration.forms.RegistrationFormUniqueEmail".
I am new to Django and a little bit overwhelmed.
Do I have to write a complete new custom user model or is there a simple solution?


Solution

  • What you need to to is extend django-registration's form class and add custom field validation, something like that:

    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
    

    You then have to override the views and make then use the form above:

    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.