Unique model field in Django and case sensitivity (postgres)

前端 未结 10 752
时光取名叫无心
时光取名叫无心 2020-12-13 13:37

Consider the following situation: -

Suppose my app allows users to create the states / provinces in their country. Just for clarity, we are considering only ASCII ch

10条回答
  •  执念已碎
    2020-12-13 14:36

    Besides already mentioned option to override save, you can simply store all text in lower case in database and capitalize them on displaying.

    class State(models.Model):
        name = models.CharField(max_length=50, unique=True)
    
        def save(self, force_insert=False, force_update=False):
            self.name = self.name.lower()
            super(State, self).save(force_insert, force_update)
    

提交回复
热议问题