On 5 août 2010, at 01:35, Samuel Clay <sam...@ofbrooklyn.com> wrote:
> I have a field that has a max_length set. When I save a model instance, and 
> the field's value is > than max_length, Django enforces that max_length at 
> the database level. (See Django docs on models: 
> http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.CharField.max_length)
> 
> However, since I am using Postgres, I receive a DatabaseError exception like 
> this:
>    DatabaseError: value too long for type character varying(1000)
> 
> I would prefer to instead auto-truncate the value (so I don't have an 
> exception). Now, I can do this manually, but what I would really want is to 
> have all of my models auto-truncate the value. (Not necessarily 
> intelligently. Just cutting it off at the 999th character is fine.) 
> 
> Should I just write a custom class that imports from models.Model and 
> override the save() method, looping through each _meta.field, checking for 
> the max_length, and then truncating? That seems inelegant and there must be a 
> better way.
Why not just create a custom field called e.g. DataCorruptingCharField which 
would silently perform this operation for you?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to