姜戈 + Haystack 搜索用户全名
我正在设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我之前只浏览过 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?
您的 UserProfile_text.txt(SearchIndex 模板)的内容是什么?
它应该类似于
您想要从该模型索引的所有字段。请参阅文档
What is the content of your UserProfile_text.txt (the SearchIndex template)?
It should be something like
with all the fields you want indexed from that model. See the documentation
关键点
基本上是您定义要在索引模型模板中索引哪些关键字,
因此请将您需要的字段添加到模板文件中
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
模板文件应该是这样的:
The template file should be like: