value too long for type character varying(N)

前端 未结 2 1230
后悔当初
后悔当初 2020-12-19 14:09

My model has a SlugField. When I try to save an instance of this model with the slug field set to a string which is longer than the field\'s max_length paramete

2条回答
  •  佛祖请我去吃肉
    2020-12-19 14:30

    Either install south and resize the column (best option), or create a pre_save signal and add code to truncate the field to 50 characters before it is saved. Something like:

    from django.db.models.signals import pre_save
    from app.model import mymodel
    
    def truncater(sender, instance, **kwargs):
        if sender is mymodel:
            if len(instance.fieldname)>50:
                instance.fieldname = instance.fieldname[:50]
    pre_save.connect(truncater, sender=mymodel)
    

提交回复
热议问题