Kenneth Falck's Blog

Longer usernames for Django

Posted on 2010-12-30 by Kenneth Falck

I just spent several hours trying to make Django accept usernames with max_length=75, in order to allow email addresses and UUIDs as usernames. By default Django limits the username to 30 characters, which is not usable for either case.

My current solution is simple; just add this to one of your apps:

from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

User._meta.get_field('username').max_length = 75 User._meta.get_field('username').validators[0].limit_value = 75 UserAdmin.form.base_fields['username'].max_length = 75 UserAdmin.form.base_fields['username'].validators[0].limit_value = 75

Then change the database to allow longer fields:


Initially I only added the first line to set the max_length attribute, but Django's admin interface won't let you save users unless you also modify the validators of the model and the form. The validators and their limit_values are created by a meta class when the original models and forms are defined, so they have to be monkey-patched afterwards.

This monkey-patch works at least with Django 1.2.4. I really wish the developers could come up with an official solution to support longer usernames, since more and more websites use email addresses as the only username.