简体   繁体   English

如何在Wagtail(django)中使用values_list?

[英]How can I use the values_list in Wagtail (django)?

hi sorry for my poor English 嗨,抱歉我的英语不好

Why wagtail search return PostgresSearchResult after search ? 为什么搜寻搜索后搜索结果返回PostgresSearchResult

i want PageQuerySet 我想要PageQuerySet

like Django postgres search backend because I can not use values_list after search 像Django postgres搜索后端一样,因为搜索后无法使用values_list

i want to get list of page path ( because i want to find pages parents(Category Page) by path ) 我想获取页面路径列表(因为我想按路径查找页面父母(类别页面))

and i can't use values_list before search because it does not work 而且我无法在搜索之前使用values_list,因为它不起作用

i know, i can use Forloop but this takes about 5 seconds for each run 我知道,我可以使用Forloop,但是每次运行大约需要5秒钟

My code is very simple : 我的代码很简单:

Django Way : #Work Django Way: #工作

ProductPage.objects.filter(title__search="phone").values_list('path')

Wagtail Way : #NotWork :( 方式: #不工作 :(

ProductPage.objects.search(query).values_list('path')

It is a bit counter intuitive but you need to always put the search(query) call last. 这有点反直观,但您需要始终将search(query)调用放在最后。

Reading the Search docs , you will see a note: 阅读搜索文档 ,您会看到一条注释:

  • The search() method will convert your QuerySet into an instance of one of Wagtail's SearchResults classes (depending on backend). search()方法会将您的QuerySet转换为Wagtail的SearchResults类之一的实例(取决于后端)。 This means that you must perform filtering before calling search() . 这意味着您必须在调用search()之前执行过滤。

This means that to get a mostly equal set of results you will need to do the following: 这意味着要获得几乎相等的结果集,您需要执行以下操作:

query = 'phone'
ProductPage.objects.values_list('path').search(query)

Note the search is the last call. 请注意, search是最后一次通话。 The search will still function as expected, I have also tested a similar situation on my setup. 搜索仍将按预期运行,我也在我的设置上测试了类似情况。

values() , values_list() , filter() all return QuerySets where as the Wagtail search() takes a QuerySet and returns a non-QuerySet object. values()values_list()filter()都返回QuerySet,而values_list() search()则采用QuerySet并返回非QuerySet对象。 The actual database querying only happens when you iterate (or use) a QuerySet so the order of the QuerySet functions do not matter. 实际的数据库查询仅在您迭代(或使用)QuerySet时发生,因此QuerySet函数的顺序无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM