Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django auth User truncating email field

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.

like image 694
Jiaaro Avatar asked May 27 '09 14:05

Jiaaro


1 Answers

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'
like image 104
rombarcz Avatar answered Sep 30 '22 17:09

rombarcz