Unique model field in Django and case sensitivity (postgres)

前端 未结 10 728
时光取名叫无心
时光取名叫无心 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:30

    You can use lookup='iexact' in UniqueValidator on serializer, like this:

    class StateSerializer(serializers.ModelSerializer): 
        name = serializers.CharField(validators=[
        UniqueValidator(
            queryset=models.State.objects.all(),lookup='iexact'
        )]
    

    django version: 1.11.6

提交回复
热议问题