Closed antonagestam closed 7 years ago
I'm getting a similar issue using google oauth:
Environment:
Request Method: GET Request URL: http://***/complete/google-oauth2/?state=gGS6LwKcStXSZxHILVRfQQyiecUeUOl6&code=4/_7U5lalbXwuUX8qok0XMjxuTyWQq.IrmImxWg29MQYKs_1NgQtmXD8TN0hwI
Django Version: 1.6.1 Python Version: 2.7.6 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'south', 'social_auth', 'holiday_tracker') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
Exception Type: DataError at /complete/google-oauth2/ Exception Value: value too long for type character varying(16)
@antonagestam @Pragueham I'm getting this issue as well. Any idea why it happened for you guys?
I'm using Google OAuth2 backend
@antonagestam @goldsmith no nothing - maybe @omab could help?
This is django.contrib.auth.models
related issue. It's because username and first_name and last_name field of auth_user table are fixed at 30 characters while user's data has longer than 30 characters.
This can be easily fixed by altering above fields to bigger one. I used varchar(75) and I didn't see any problem so far. If you are using all Django form, you might need to let Django know too. For example,
User._meta.get_field_by_name('username')[0].max_length = 75
User._meta.get_field('username').validators[0].limit_value = 75
User._meta.get_field_by_name('first_name')[0].max_length = 75
User._meta.get_field('first_name').validators[0].limit_value = 75
User._meta.get_field_by_name('last_name')[0].max_length = 75
User._meta.get_field('last_name').validators[0].limit_value = 75
You can also add a pipeline entry before the user instance is created, and modify the values in details
to fit your database limits.
Thanks, I'm obviously being thick, but having read the docs, I still don't understand @omab 's solution. Can you please explain a little more?
@Pragueham, create a pipeline function (docs at http://django-social-auth.readthedocs.org/en/latest/pipeline.html) like this:
from django.contrib.auth.models import User
from django.db.models.fields import FieldDoesNotExist
def clean_user_details(backend, details, user=None, *args, **kwargs):
if user is not None:
return
details_copy = details.copy()
for field_name, field_value in details.items():
try:
field = User._meta.get_field(field_name)
except FieldDoesNotExist:
pass
else:
if len(field_value) > field.max_length:
details_copy[field_name] = field_value[:field.max_length]
return details_copy
And put it before the create_user
entry in the pipeline. That pipeline will enforce your fields limits to the values that will be used later to create the user.
@omab is above modification in pipeline is still the best possible solution? I am running into similar issue with username field coming from google social signin, and would like to add truncation so that user creation doesn't fail.
DatabaseError: value too long for type character varying(30)