Django: Query using contains each value in a list

后端 未结 5 1563
广开言路
广开言路 2020-11-27 12:07

I need to perform a django query that checks if a field contains all values within a list. The list will be of varying length

Example

User.objects.fi         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 12:33

    import operator
    from django.db.models import Q
    
    User.objects.filter(reduce(operator.and_, (Q(first_name__contains=x) for x in ['x', 'y', 'z'])))
    

    for python 3

    from functools import reduce
    

    .

提交回复
热议问题