data too long in a CharField column

818 views
Skip to first unread message

MS

unread,
May 4, 2009, 11:46:53 AM5/4/09
to Django users
Hi,

I have a problem with django+postgresql:
I have a model with a CharField(max_length=255) field, and I'm
assigning some much longer value to this field, like:

m = MyModel()
m.myfield = 'very long text - say 400 chars'
m.save()

In save() I'm getting an error "ERROR: value too long for type
character varying(255)", and the SQL statement contains the 'very long
text - say 400 chars' intact.

I thought that if django knows that a field is restricted to 255
chars, then it will validate it and
truncate excessive chars before saving. But it doesn't. How can I get
rid of that problem?

Thanks,
MS

George Song

unread,
May 4, 2009, 11:53:13 AM5/4/09
to django...@googlegroups.com

If you want that behavior, just truncate `myfield` in `MyModel.save()`
before calling the super save.

<http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.save>

--
George

MS

unread,
May 4, 2009, 2:28:20 PM5/4/09
to Django users
>
> If you want that behavior, just truncate `myfield` in `MyModel.save()`
> before calling the super save.
>
> <http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db....>



Well, if that's THE solution, then I'll post my implementation - maybe
someone will find it useful:


def autotruncate(cls):
oldsave = cls.save
def newsave(self, **kwargs):
for f in self._meta._fields():
if isinstance(f, models.CharField):
val = getattr(self, f.name)
if val is not None:
setattr(self, f.name, val[:f.max_length])
oldsave(self, **kwargs)
cls.save = newsave


To use it, write:
autotruncate(MyModel)

Any comments are welcome.

Regards,
MS

pbzRPA

unread,
May 4, 2009, 2:44:40 PM5/4/09
to Django users
Hi,

I am not sure if truncating the data is a good solution as the user
will be under the impression that all the text was saved whereas only
the first 255 was really saved.

You can add a max_length to your form or modelform field which will
block the user from typing more then the max length.

regards.

Malcolm Tredinnick

unread,
May 4, 2009, 3:38:27 PM5/4/09
to django...@googlegroups.com

As noted elsewhere in the thread, truncating would be almost always the
wrong solution, since it quietly throws away data.

As for detecting the problem in the first place, form fields will
already pick up this problem if the user is providing data through a
form (it will be a validation error). Right now, model field validation
doesn't exist, so you, as the programmer populating the field, is
expected to make sure the data fits (in the above example, you're
populating the model field directly, so you need to check you're
populating it with something sensible). In fact, even after model field
validation exists, it will still be your responsibility to make sure the
data fits: Django will raise a validation error if it doesn't, but it
won't automatically fix the problem because an appropriate solution is
going to be domain specific.

Regards,
Malcolm

parkprimus

unread,
May 5, 2009, 8:02:26 AM5/5/09
to Django users
According to django's documentation, "The max_length is enforced at
the database level and in Django's validation.". Can you verify that
the max_length is configured in you database structure?

MS

unread,
May 5, 2009, 9:44:07 AM5/5/09
to Django users
Hi,

On 5 Maj, 14:02, parkprimus <parkpri...@gmail.com> wrote:
> According to django's documentation, "The max_length is enforced at
> the database level and in Django's validation.".  Can you verify that
> the max_length is configured in you database structure?

Yes, I have it. AFAIK max_length is required in CharFields.

Regarding other replies - I know it's wrong to truncate data but I
need it in some background/batch processing, so it's not for forms and
user input.

Thanks,
MS

hans alexander

unread,
Aug 21, 2020, 9:30:10 AM8/21/20
to Django users
I changed my database from mysql to postgresql. 
But when I tried saving data on forms, I got this error " value too long for type character varying(50)"
I don't know why, because when I was using MYSQL, I didn't get any error.
Can someone help me with this Postgresql?
Reply all
Reply to author
Forward
0 new messages