Django: Query using contains each value in a list

后端 未结 5 1575
广开言路
广开言路 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:30

    Just another approach.

    qs = User.objects.all()
    for search_term in ['x', 'y', 'z']:
        qs = qs.filter(first_name__contains=search_term)
    

    I'm not sure if it is better, but it's more readable.

提交回复
热议问题