推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
larkifly
V2EX  ›  Python

django 数据库查询 filter 如何区分大小写

  •  
  •   larkifly · Oct 12, 2014 · 9799 views
    This topic created in 4417 days ago, the information mentioned may be changed or developed.
    我试了__exact方法,但是没用,依然没有区分大小写
    8 replies  •  2014-10-13 16:13:49 +08:00
    zeq
    zeq
    Oct 12, 2014
    1
    你的代码是怎样的
    raquelken
    raquelken
    Oct 12, 2014
    2
    @larkifly
    iexact
    larkifly
    larkifly
    OP
    Oct 13, 2014
    3
    @zeq
    usable = CarCardList.objects.filter(car_card__iexact=code)
    usable = CarCardList.objects.filter(car_card__exact=code)

    class CarCardList(models.Model):
    car_card = models.CharField(max_length=255, unique=True)
    is_used = models.BooleanField(default=False)
    get_from = models.ForeignKey(Merchant)
    used_time = models.IntegerField(null=True)
    user = models.ForeignKey(User, null=True)
    def __unicode__(self):
    return self.car_card

    这两个方法都没用,环境是windows mysql
    zeq
    zeq
    Oct 13, 2014❤️1
    4
    这是因为mysql的查询默认是不区分大小写的,你可以在匹配语句钱加上 BINARY 来区分大小写,例如

    SELECT * WHERE BINARY title='django';

    可以供你参考的一个方法是:

    CarCardList.objects.extra(where=['binary car_card=%s'], params=[code])

    https://docs.djangoproject.com/en/dev/ref/models/querysets/#extra
    y10n
    y10n
    Oct 13, 2014
    5
    数据库看看呢

    如果类似 utf8_general_ci 改成 utf8_general_cs
    __exact用的是=
    __iexact用的是LIKE,效率也考虑下哦
    iewgnaw
    iewgnaw
    Oct 13, 2014
    6
    应该是数据库大小写不敏感,你直接用数据库命令查询试试看呢
    xianglong1025
    xianglong1025
    Oct 13, 2014
    7
    数据库不支持大小写敏感
    larkifly
    larkifly
    OP
    Oct 13, 2014
    8
    @iewgnaw
    @y10n
    @zeq
    数据库配置错误
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5166 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:32 · PVG 10:32 · LAX 18:32 · JFK 21:32
    ♥ Do have faith in what you're doing.