和使用相同关键字的条件Django查询 [英] And Condition Django Query Using Same Keyword

查看:186
本文介绍了和使用相同关键字的条件Django查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有出版物模型和标签模型的Django应用程序。每个出版物都有一个或多个与之相关联的标签。我想用一组两个标签来查询数据库,只返回那些具有这些标签的两个标签的发布。



我似乎找不到这个语法,虽然我确定它很容易获得 - 我想我没有使用正确的语言来搜索。我已经尝试过的是:

  pubs_for_tags = Publication.objects.filter(tags__title__istartswith = q,tags__title__istartswith = q2)

但这给我一个错误关键字参数重复。我也尝试了一些这样的变化,但迄今没有任何工作。有人可以用这个正确的语法启发我吗?

解决方案

  pubs_for_tags = Publication.objects.filter(tags__title__istartswith = q).filter(tags__title__istartswith = q2)





  pubs_for_tags = Publication.objects.filter(Q(tags__title__istartswith = q),Q(tags__title__istartswith = q2))


I have a Django application with a Publication model and a Tag model. Each publication has one or more Tags associated with it. I want to query the database with a set of two Tags and have returned only publications that have BOTH of those tags.

I cannot seem to find the syntax for this although I am certain it is readily available - I suppose I am not using the correct language to search. What I have tried already is:

pubs_for_tags = Publication.objects.filter(tags__title__istartswith=q, tags__title__istartswith=q2)

But this gives me an error "keyword argument repeated". I've also tried some variations of this, but nothing has worked so far. Can someone enlighten me on the correct syntax for this?

解决方案

pubs_for_tags = Publication.objects.filter(tags__title__istartswith=q).filter( tags__title__istartswith=q2)


or

pubs_for_tags = Publication.objects.filter(Q(tags__title__istartswith=q), Q( tags__title__istartswith=q2))

这篇关于和使用相同关键字的条件Django查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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