9

I have an issue with the django.contrib.auth User model where the email max_length is 75.

I am receiving email addresses that are longer than 75 characters from the facebook api, and I need to (would really like to) store them in the user for continuity among users that are from facebook connect and others.

I am able to solve the problem of "Data truncated for column 'email' at row 1" by manually going editing the field in our mySql database, but is there a better way to solve this? preferably one that does not involve me manually editing the database every time I reset it for a schema change?

I am ok with editing the database as long as I can add it to the reset script, or the initial_data.json file.

niton
  • 8,771
  • 21
  • 32
  • 52
Jiaaro
  • 74,485
  • 42
  • 169
  • 190

2 Answers2

12

EmailField 75 chars length is hardcoded in django. You can fix this like that:

from django.db.models.fields import EmailField
def email_field_init(self, *args, **kwargs):
  kwargs['max_length'] = kwargs.get('max_length', 200)
  CharField.__init__(self, *args, **kwargs)
EmailField.__init__ = email_field_init

but this will change ALL EmailField fields lengths, so you could also try:

from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.db import models
User.email = models.EmailField(_('e-mail address'), blank=True, max_length=200)

both ways it'd be best to put this code in init of any module BEFORE django.contrib.auth in your INSTALLED_APPS

Since Django 1.5 you can use your own custom model based on AbstractUser model, therefore you can use your own fields & lengths. In your models:

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    email = models.EmailField(_('e-mail address'), blank=True, max_length=200)

In settings:

AUTH_USER_MODEL = 'your_app.models.User'
rombarcz
  • 1,604
  • 13
  • 19
  • 1
    ick... that sucks :( why does facebook give me these gigantic email addresses? – Jiaaro May 27 '09 at 15:06
  • well, kinda sucks, anyway you have other choice: write your own django.contrib.auth module (which btw isn't worst thing to do if you see more troubles like this with buildin auth) – rombarcz May 27 '09 at 21:03
  • Don't work anymore. It wont let you do this until after apps loaded, and errors out with "Apps aren't loaded yet" – Shayne May 28 '19 at 07:29
2

There is now a ticket to increase the length of the email field in Django: http://code.djangoproject.com/ticket/11579

Graham King
  • 5,710
  • 3
  • 25
  • 23