Django query case-insensitive list match

前端 未结 7 1148
独厮守ぢ
独厮守ぢ 2020-12-03 09:21

I have a list of names that I want to match case insensitive, is there a way to do it without using a loop like below?

a = [\'name1\', \'name2\', \'name3\']
         


        
相关标签:
7条回答
  • 2020-12-03 10:25

    Here is an example of custom User model classmethod to filter users by email case-insensitive

    from django.db.models import Q
    
    @classmethod
    def get_users_by_email_query(cls, emails):
        q = Q()
        for email in [email.strip() for email in emails]:
            q = q | Q(email__iexact=email)
        return cls.objects.filter(q)
    
    0 讨论(0)
提交回复
热议问题