-
-
Save andkon/8a27e43b87a414cb92c6 to your computer and use it in GitHub Desktop.
Changing django's username AND email field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This code is a suggested answer to question asked on Stackoverflow: | |
http://stackoverflow.com/questions/4827965/can-i-change-djangos-auth-user-username-field-to-be-100-chars-long-without-break/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# authhacks/username_length.py | |
import sys | |
def hack_models(length=72): | |
from django.contrib.auth.models import User | |
username = User._meta.get_field("username") | |
username.max_length = length | |
hack_validators(username.validators, length=length) | |
email = User._meta.get_field("email") | |
email.max_length = length | |
hack_validators(email.validators, length=length) | |
def hack_forms(length=72, forms=[ | |
'django.contrib.auth.forms.UserCreationForm', | |
'django.contrib.auth.forms.UserChangeForm', | |
'django.contrib.auth.forms.AuthenticationForm', | |
]): | |
""" | |
Hacks username length in django forms. | |
""" | |
for form in forms: | |
modulename, sep, classname = form.rpartition('.') | |
if not modulename in sys.modules: | |
__import__(modulename) | |
module = sys.modules[modulename] | |
klass = getattr(module, classname) | |
hack_single_form(klass, length) | |
def hack_single_form(form_class, length=72): | |
if hasattr(form_class, 'declared_fields'): | |
fields = form_class.declared_fields | |
elif hasattr(form_class, 'base_fields'): | |
fields = form_class.base_fields | |
else: | |
raise TypeError('Provided object: %s doesnt seem to be a valid Form or ' | |
'ModelForm class.' % form_class) | |
username = fields['username'] | |
hack_validators(username.validators) | |
username.max_length = length | |
username.widget.attrs['maxlength'] = length | |
if 'email' in fields: | |
email = fields['email'] | |
hack_validators(email.validators) | |
email.max_length = length | |
email.widget.attrs['maxlength'] = length | |
def hack_validators(validators, length=72): | |
from django.core.validators import MaxLengthValidator | |
for key, validator in enumerate(validators): | |
if isinstance(validator, MaxLengthValidator): | |
validators.pop(key) | |
validators.insert(0, MaxLengthValidator(length)) | |
hack_models(200) | |
hack_forms(200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# authhacks/__init__.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# settings.py | |
INSTALLED_APPS = [ | |
# ... | |
'django.contrib.auth', | |
'authhacks' | |
# ... | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment