使用|创建动态对象分隔符python [英] Creating dynamic objects with | separator python

查看:46
本文介绍了使用|创建动态对象分隔符python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决的是这样的问题

What I want is to solve is something like this

names = ['Aleister', 'Matovu']
args = (Q(name__contains=name[0])|Q(name__contains=name[1]))
queryset.complex_filter(args)

问题是我有一个动态对象名称,并且其长度不固定.我想工作的是,如果我遍历名称并创建一个动态的 args 对象,但是我不确定那是什么类型的对象.我不确定该怎么做,所以被困住了.

What the problem is I have a names as a dynamic object and its length is not fixed. What I am thinking would work is if I looped though the names and created a dynamic args object but I am not sure what kind of object that is. I am not sure how exactly to do that, so I am stuck.

如果您能解决这个问题,那将是一件很酷的事情,或者如果您可以给我一种替代的方法来解决同样棒的情况,那将是一件很酷的事情.谢谢

If you can help around that, that will be really cool or if you can give me an alternative way to go about the same scenario that would be awesome. Thank you

推荐答案

import operator
names = [...]
query = reduce(operator.or_, [Q(name__icontains=name) for name in names])
results = queryset.complex_filter(query)

这篇关于使用|创建动态对象分隔符python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆