DataError value too long for type character varying(100)

Raised by:

django/db/backends/util.py (line 53)

Traceback (most recent call last):
  File "python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "python2.7/site-packages/django/db/transaction.py", line 371, in inner
    return func(*args, **kwargs)
  File "python2.7/site-packages/django/contrib/admin/options.py", line 465, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "python2.7/site-packages/django/contrib/admin/sites.py", line 198, in inner
    return view(request, *args, **kwargs)
  File "python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
    return bound_func(*args, **kwargs)
  File "python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
    return func(self, *args2, **kwargs2)
  File "python2.7/site-packages/django/db/transaction.py", line 371, in inner
    return func(*args, **kwargs)
  File "python2.7/site-packages/django/contrib/admin/options.py", line 1264, in change_view
    self.save_related(request, form, formsets, True)
  File "python2.7/site-packages/django/contrib/admin/options.py", line 917, in save_related
    self.save_formset(request, form, formset, change=change)
  File "python2.7/site-packages/django/contrib/admin/options.py", line 905, in save_formset
    formset.save()
  File "python2.7/site-packages/django/forms/models.py", line 618, in save
    return self.save_existing_objects(commit) + self.save_new_objects(commit)
  File "python2.7/site-packages/django/forms/models.py", line 747, in save_new_objects
    self.new_objects.append(self.save_new(form, commit=commit))
  File "python2.7/site-packages/django/forms/models.py", line 877, in save_new
    obj.save()
  File "python2.7/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "python2.7/site-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "python2.7/site-packages/django/db/models/sql/compiler.py", line 903, in execute_sql
    cursor.execute(sql, params)
  File "python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
DataError: value too long for type character varying(100)

How to reproduce

on models.py

class ModelWithImage(models.Model):
    image = models.ImageField()

with python manage.py shell, run

from example.models import ModelWithImage
x = ModelWithImage(image='a' * 300)
x.save()

How to fix

Some library or yourself is trying to insert a value in a field that has max_length=100 without a form or without cleaning the form, so the error is happening at db level. The field in question is probably a FileField or ImageField since their default max_length is 100 [1], which might not be enough for large paths.

The easiest solution is to increase field max_length to a value that is enough for your use case (ex: 255).

[1] https://docs.djangoproject.com/en/1.6/ref/models/fields/#filefield