【问题标题】:Implement Django __iin custom lookup实现 Django __iin 自定义查找
【发布时间】:2017-08-20 15:32:04
【问题描述】:

我阅读了Django documentation on custom lookups,但未能弄清楚如何实现不区分大小写的 __in 查找。

谁能帮忙?我在 Django 1.10 上使用 Postgresql 数据库。

【问题讨论】:

    标签: django


    【解决方案1】:

    不需要__iin 查找实现。你可以像这样使用iregex

    result = MyModel.objects.filter(field__iregex=r'(test1|test2|test3)')
    

    甚至更通用的方法:

    a = ['test1', 'test2', 'test3']
    to_lookup = '|'.join(a)  # 'test1|test2|test3'
    
    result = MyModel.objects.filter(field__iregex=r'(' + to_lookup + ')')
    

    【讨论】:

    • 谢谢,但如果您需要多次查找,这对我来说似乎是一个丑陋的解决方案。
    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    相关资源
    最近更新 更多