Max_length of EmailField in django.contrib.auth.models.User

422 views
Skip to first unread message

somen...@gmail.com

unread,
Mar 22, 2015, 8:37:04 AM3/22/15
to django...@googlegroups.com

Anderson Resende

unread,
Mar 22, 2015, 10:28:34 AM3/22/15
to django...@googlegroups.com
No, the user class uses 75. You need to change to 254.

models.EmailField(max_lenght=254)

somen...@gmail.com

unread,
Mar 22, 2015, 10:45:34 AM3/22/15
to django...@googlegroups.com
Thanks, Anderson, but can I overwrite the length of User class? How? Sorry I'm still a newbee? With AbstractUser? [https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#django.contrib.auth.models.CustomUser] or Extending user class [https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#extending-the-existing-user-model]?

On the other, hand, it were useful if someone could add the information in the web page.

El diumenge, 22 març de 2015 15:28:34 UTC+1, Anderson Resende va escriure:

Tim Graham

unread,
Mar 22, 2015, 10:55:33 AM3/22/15
to django...@googlegroups.com
Currently you need to use a custom user model if you want to change the username max_length. There's an open ticket to increase the default length to 254 though: https://code.djangoproject.com/ticket/20846

François Schiettecatte

unread,
Mar 22, 2015, 11:04:37 AM3/22/15
to django...@googlegroups.com
Hi

I have been using this patch to get around the short user name (which is 30 characters):

#--------------------------------------------------------------------------
#
# Patch to allow username to exceed the default maximum of 30 characters
#
# http://stackoverflow.com/a/6453368/1228131
#
# Note: this works for django 1.3, 1.4, 1.5, 1.6
#

# Import what we need
from django.contrib.auth.models import User
from django.core.validators import MaxLengthValidator

# New username length
NEW_USERNAME_MAX_LENGTH = 75

# Method to patch the user name maximum length
def monkey_patch_username_maximum_length():

username = User._meta.get_field("username")
username.max_length = NEW_USERNAME_MAX_LENGTH

for validator in username.validators:
if isinstance(validator, MaxLengthValidator):
validator.limit_value = NEW_USERNAME_MAX_LENGTH

# Patch the user name maximum length
monkey_patch_username_maximum_length()

#--------------------------------------------------------------------------
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/012b584e-3e72-44e6-9a09-4909b2b3353c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

somen...@gmail.com

unread,
Mar 22, 2015, 11:21:13 AM3/22/15
to django...@googlegroups.com
Is it the same if I want to specify email as required field, isn't?

El diumenge, 22 març de 2015 15:55:33 UTC+1, Tim Graham va escriure:

Anderson Resende

unread,
Mar 22, 2015, 1:04:05 PM3/22/15
to django...@googlegroups.com
You can inheriant of AbstractBaseUser and define your field:

from django.contrib.auth.models import AbstractBaseUser

class MyCustomUser(AbstractBaseUser):
     mail = models.EmailField(max_lenght=254)

    REQUIRED_FIELDS = ['mail']
     # more code
 

Please sorry about my english, I'm a Brazilian guy rsrsrs

Anderson Resende

unread,
Mar 22, 2015, 1:27:33 PM3/22/15
to django...@googlegroups.com
I will give you one more complete example:
Custom a User is not easy you need to do many things:

In that link django-docs explain all for you and there is an example with email field.

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#a-full-example


My tip is: In the docs there is many other fields like birth_date that you can remove.
Second, In docs the email field has used with username, you can create your own username field and in
USERNAME_FIELD = 'email' change for USERNAME_FIELD = 'myusername'

Reply all
Reply to author
Forward
0 new messages