django icontains with __in lookup

前端 未结 3 1906
庸人自扰
庸人自扰 2020-12-05 01:14

So I want to find any kind of matching given some fields, so for example, this is what I would like to do:

possible_merchants = [\"amazon\", \"web\", \"servi         


        
3条回答
  •  温柔的废话
    2020-12-05 01:45

    This is the approach that I adopted:

    class MyManager(models.Manager):
        def exclusive_in(self, lookup, value_list):
            return self.filter(reduce(or_, (Q(**{lookup:_}) for _ in value_list)))
    

    Here is now to use it:

    Companies.objects.exclusive_in('name__icontains', possible_merchants])
    

    It was inspired by other answers in this thread, as well as Django filter queryset __in for *every* item in list.

提交回复
热议问题