姜戈 + Haystack 搜索用户全名

发布于 2024-10-06 23:09:05 字数 770 浏览 0 评论 0原文

我正在设置 django-haystack,只是有一个问题。

我使用 django-profiles,它允许我添加单独的信息,即城市、性别等。

当我仅执行搜索时,我为我的 UserProfile 创建了一个 search_indexes.py似乎返回用户用户名结果,即。

如果我输入 john 并且用户的用户名是 john 那么它就会被选中,如果我输入 James 并且用户 john 的名字是James 它不返回结果。

我的 search_indexes.py

from haystack.indexes import *
from haystack import site
from models import UserProfile

class UserProfileIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    user = CharField(model_attr='user', use_template=True)

    def prepare_user(self, obj):
        return "%s <%s>" % (obj.user.get_full_name(), obj.user.email)

site.register(UserProfile, UserProfileIndex)

I am setting up django-haystack and just have a question.

I use django-profiles, which allows me to add seperate information ie, city, gender etc.

I created a search_indexes.py for my UserProfile when I perform a search it only seems to return a users username result ie.

if I type john and the users username is john then it gets picked up, if I type James and the user john's first name is James it does not return a result.

my search_indexes.py

from haystack.indexes import *
from haystack import site
from models import UserProfile

class UserProfileIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    user = CharField(model_attr='user', use_template=True)

    def prepare_user(self, obj):
        return "%s <%s>" % (obj.user.get_full_name(), obj.user.email)

site.register(UserProfile, UserProfileIndex)

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

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

发布评论

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

评论(4

随遇而安 2024-10-13 23:09:05

我之前只浏览过 haystack 代码,但这不是因为您在模型属性“user”上建立索引,这意味着这将是唯一搜索的字段吗?

I've only glanced over haystack code before, but isn't it because you're indexing on the model attribute "user" which means that will be the only field searched?

贱人配狗天长地久 2024-10-13 23:09:05

您的 UserProfile_text.txt(SearchIndex 模板)的内容是什么?
它应该类似于

{{ object.username }}
{{ object.firstname }}
...

您想要从该模型索引的所有字段。请参阅文档

What is the content of your UserProfile_text.txt (the SearchIndex template)?
It should be something like

{{ object.username }}
{{ object.firstname }}
...

with all the fields you want indexed from that model. See the documentation

烟─花易冷 2024-10-13 23:09:05

关键点

基本上是您定义要在索引模型模板中索引哪些关键字,

因此请将您需要的字段添加到模板文件中

the key point is

basicly you define what keywords you wantted to be indexed in your template of your index model

so please add fields that you need into your template file

匿名的好友 2024-10-13 23:09:05

模板文件应该是这样的:

{{ object.user.first_name}}
{{ object.user.middle_name }}

The template file should be like:

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