如何将列表转换为多个Q对象?
服用 from Django 3.2文档,我需要将这样的参数用于.filter
:
Q(Question__StartSwith ='Who')| Q(Question__StartSwith ='What')
在我的情况下,我需要转换每个用户的选择,我通过request.meta ['query_string']
,进入自己的q()
对象。
如果我尝试从该参数列表中创建Q对象,则它将无法使用,因为将评估|
。这似乎一定是解决问题的问题,但是我还没有找到答案。感谢您的建议。
Taking an example from Django 3.2 documentation, I need to use an argument like this for .filter
:
Q(question__startswith='Who') | Q(question__startswith='What')
In my case need to convert each of the user's selections, which I'm getting in views.py via request.META['QUERY_STRING']
, into it's own Q()
object.
If I tried to create Q objects from that list of parameters, it would not work, because the |
would be evaluated. This seems like it must be a solved a problem, but I haven't had luck yet finding the answer. Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在循环中建立复杂的
q
对象 - 使用q_obj | = q(...)
with或You can build up a complex
Q
object in a loop - useq_obj |= Q(...)
to add anotherQ
with OR您可以使用Distnict()来做到这一点
You can do that using distnict()
使用Regex可能是一个简单的选择:
using regex might be an easy option: