Django LowerCaseCharField

后端 未结 4 981
我在风中等你
我在风中等你 2021-02-13 17:22

We implemented a LowerCaseCharField. We would be happy to hear better implementation suggestions.

from django.db.models.fields import CharField

class LowerCaseC         


        
4条回答
  •  面向向阳花
    2021-02-13 17:50

    Another way to do it would be to add a pre_save hook to your model, and then just convert all the fields you want to be lowercase to lowercase there using lower(). This is discussed here in a slightly different fashion. This way, any time the object is saved its field is converted to lower case.

    Example:

    @receiver(pre_save, sender=YourModel)
    def convert_to_lowercase(sender, instance, *args, **kwargs):
        instance.lowercase_field = instance.lowercase_field.lower()
    

提交回复
热议问题