使用 ResultSet 访问实际对象 从 Djapian 上的索引搜索返回命中对象

发布于 2024-09-08 17:10:32 字数 309 浏览 1 评论 0原文

我希望能够在查询时返回实际的对象列表,而不是带有 Hit 对象的 ResultSet。

示例:

indexer.search(word).prefetch()

这将返回一个具有匹配命中的 ResultSet 对象,但我希望能够访问这些对象本身。类似于:

model.objects.filter(name__icontains=word)

会返回。 我不只使用第二个选项的原因是我无法搜索不同模型的交叉对象,这可以通过索引来完成。

谢谢

I want to be able to have an actual list of objects returned when I query, instead of a ResultSet with Hit objects.

Example:

indexer.search(word).prefetch()

this would return a ResultSet object with matching hits, but I would like to have access to the objects themselves. Similar to what:

model.objects.filter(name__icontains=word)

would return.
the reason I don't just use the second option is that I cannot search cross-objects of different models, which can be done with indexing.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2024-09-15 17:10:32

我想通了。对象最终被返回。可以通过以下方式访问它:

results = indexer.search(word).prefetch()
for hit in results:
    print hit.instance.model_attribute

其中 model_attribute 是返回对象的现有变量

我相信类函数也可以在 model_attribute 位置调用(最后添加括号),

但请注意搜索跨模型时,不同模型可能存在或不存在“model_attribute”。所以这可能会导致错误。

我希望 djapian 有更多关于此的文档,因为我根本找不到它

I figured it out. The object is returned afterall. It can be accessed with:

results = indexer.search(word).prefetch()
for hit in results:
    print hit.instance.model_attribute

where model_attribute would be an existing variable of the object returned

I believe class functions can be called upon as well in the model_attribute spot, (with bracket added in the end)

but note that when searching cross-models, the 'model_attribute' may or may not exist for different models. So that may lead to errors.

I wish djapian had more documentation on this, as I couldn't find it at all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文