I have a field with the max_length set. When I save a model instance and the field value is greater than max_length , Django applies this max_length parameter 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 use Postgres, I get a DatabaseError exception similar to this:
DatabaseError: value too long for type character varying(1000)
I would rather instead automatically trim the value (so I have no exception). Now I can do it manually, but I would really like all my models to automatically truncate the value. (Not necessarily reasonable. Just cut it off on the 999th character in order.)
Should I just write a custom class that imports from models.Model and override the save () method, iterate through each _meta.field, check max_length and then truncate? It seems inelegant, and there should be a better way.
django postgresql django-models
Samuel Clay Aug 11 2018-10-10T00: 10-10
source share