Django:同一模型上不同查询集的联合
我正在对模型进行搜索编程,但遇到了问题。
我的模型几乎是这样的:
class Serials(models.Model):
id = models.AutoField(primary_key=True)
code = models.CharField("Code", max_length=50)
name = models.CharField("Name", max_length=2000)
并且我在数据库中有这样的元组:
1 BOSTON The new Boston
2 NYT New York journal
3 NEWTON The old journal of Mass
4 ANEWVIEW The view of the young people
如果我搜索字符串new
,我想要的是:
- 首先是开始的
names
包含字符串 - 然后是以字符串开头的
codes
- 然后是包含该字符串的
names
- 然后是包含该字符串的
codes
所以前面的列表应该以以下方式出现:
2 NYT New York journal
3 NEWTON The old journal of Mass
1 BOSTON The new Boston
4 ANEWVIEW The view of the young people
我发现获得这种结果的唯一方法是进行不同的搜索(如果我在单个搜索中输入“OR”,我就会失去我想要的顺序)。
我的问题是,显示结果的模板代码确实是多余的,而且老实说非常难看,因为我必须为所有 4 个不同的查询集重复相同的代码。最糟糕的是我无法使用分页!
现在,由于不同查询集的结构是相同的,我想知道是否有一种方法可以连接 4 个查询集并只为模板提供一个查询集。
I'm programming a search on a model and I have a problem.
My model is almost like:
class Serials(models.Model):
id = models.AutoField(primary_key=True)
code = models.CharField("Code", max_length=50)
name = models.CharField("Name", max_length=2000)
and I have in the database tuples like these:
1 BOSTON The new Boston
2 NYT New York journal
3 NEWTON The old journal of Mass
4 ANEWVIEW The view of the young people
If I search for the string new
, what I want to have is:
- first the
names
that start with the string - then the
codes
that start with the string - then the
names
that contain the string - then the
codes
that contain the string
So the previous list should appear in the following way:
2 NYT New York journal
3 NEWTON The old journal of Mass
1 BOSTON The new Boston
4 ANEWVIEW The view of the young people
The only way I found to have this kind of result is to make different searches (if I put "OR" in a single search, I loose the order I want).
My problem is that the code of the template that shows the result is really redundant and honestly very ugly, because I have to repeat the same code for all the 4 different querysets. And the worse thing is that I cannot use the pagination!
Now, since the structure of the different querysets is the same, I'm wandering if there is a way to join the 4 querysets and give the template only one queryset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以进行这四个查询,然后将它们链接到您的程序中:
但这似乎不太好,因为您必须进行查询。
您还可以使用原始 sql 编写自己的 sql,例如:
另请参阅:
如何在 Django 视图中组合 2 个或更多查询集?
You can make those four queries and then chain them inside your program:
but this doesn't seem to nice because your have to make for queries.
You can also write your own sql using raw sql, for example:
Also look at this:
How to combine 2 or more querysets in a Django view?
您还应该能够执行 qs1 | qs2 | qs3 | qs4。然而,这会给你重复的内容。
您可能想要研究的是 Q() 对象:
我不确定如果您这样做,它是否会处理排序,因为这将依赖于数据库这样做。
事实上,即使使用 qs1 | qs2 可能会导致顺序由数据库确定。这可能是缺点(也是您可能需要至少两个查询的原因)。
You should also be able to do
qs1 | qs2 | qs3 | qs4
. This will give you duplicates, however.What you might want to look into is
Q()
objects:I'm not sure if it will handle the ordering if you do it this way, as this would rely on the db doing that.
Indeed, even using
qs1 | qs2
may cause the order to be determined by the db. That might be the drawback (and reason why you might need at least two queries).