1

我正在尝试为我的 feinCMS 页面构建一个简单的搜索。在本教程的帮助下,我创建了一个搜索所有页面标题的搜索:

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))

也许不是最好的解决方案,但它确实有效。

4

0 回答 0