Django区分大小写条目

阿比舍克

我有以下查询:

>>> z = Restaurant.objects.values_list('city',flat=True).order_by('city').distinct()
>>> z
[u'ELURU', u'Eluru', u'Hyderabad']

如您所见,由于区分大小写,它并不是完全不同的。我该如何解决这个问题?

JxAxMxIxN

您可以将注释Lower(或Upper等)结合使用,以规范化您的值并像这样返回真正不同的值...

from django.db.models.functions import Lower

z = Restaurant.objects.annotate(
   city_lower=Lower('city')).values_list(
      'city_lower',flat=True).order_by('city_lower').distinct()

注意:请确保将order_by设置为“ city_lower”,而不是“ city”,以避免重复。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章