我正在尝试构建一个简单的搜索我的feinCMS页面。 在this tutorial的帮助下,我创建了一个搜索所有页面标题的搜索:
def get_results(query_string, search_fields):
''' Returns a query, that is a combination of Q objects. That combination
aims to search keywords within a model by testing the given search fields.
'''
if len(query_string)>1:
results = []
query = None # Query to search for every search term
terms = normalize_query(query_string)
for term in terms:
results += Page.objects.filter(title__contains = term)
return results
但我不知道如何在各种内容类型中搜索。
现在我添加了markdowncontent_set = Page.content_type_for(MarkdownContent)
但是我收到了什么?我如何循环浏览内容并检查它是否包含query_sring
?
修改
现在我的代码看起来像这样:
def get_results(query_string, search_fields):
''' Returns a query, that is a combination of Q objects. That combination
aims to search keywords within a model by testing the given search fields.
'''
if len(query_string)>0:
results = []
query = None # Query to search for every search term
terms = normalize_query(query_string)
for term in terms:
results += Page.objects.filter(title__contains = term)
results += Page.objects.select_related("markdowncontent_set").filter(markdowncontent_set__content__contains = term)
return list(set(results))
也许不是最好的解决方案,但它有效。