gpt4 book ai didi

python - 管理中的 Django _unaccent 和 _search

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:10 25 4
gpt4 key购买 nike

所以我找到了一种在 Django 过滤器中一起使用 _unaccent_search 的方法,但我正在尝试找到一种方法将其正确实现到搜索栏中在管理页面中。到目前为止,我有以下代码,但由于我没有正确完成,因此 search_fields = [] 等管理设置未正确应用。如果有人可以帮助我将我的解决方案与原始 Django 实现合并,我将不胜感激。

admin.ModelAdmin 函数被覆盖:

def get_search_results(self, request, queryset, search_term):
"""
Returns a tuple containing a queryset to implement the search,
and a boolean indicating if the results may contain duplicates.
"""
# Apply keyword searches.
def construct_search(field_name):
if field_name.startswith('^'):
return "%s__istartswith" % field_name[1:]
elif field_name.startswith('='):
return "%s__iexact" % field_name[1:]
elif field_name.startswith('@'):
return "%s__search" % field_name[1:]
else:
return "%s__icontains" % field_name

use_distinct = False
search_fields = self.get_search_fields(request)
if search_fields and search_term:
orm_lookups = [construct_search(str(search_field))
for search_field in search_fields]
for bit in search_term.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
queryset = queryset.filter(reduce(operator.or_, or_queries))
if not use_distinct:
for search_spec in orm_lookups:
if lookup_needs_distinct(self.opts, search_spec):
use_distinct = True
break

return queryset, use_distinct

我的代码:

def get_search_results(self, request, queryset, search_term):  # TODO: Make this more professionally implemented (proper overrides)
""" Overrides default search completely to incorporate __search and __unaccent lookups """
use_distinct = False

if search_term: # Note: "if" condition necessary to show ALL results in admin if not search_term is specified (otherwise shows 0 results)
queryset = queryset.annotate(unaccent_title=SearchVector('title', config='english_unaccent')).filter(unaccent_title=SearchQuery(search_term, config='english_unaccent'))

return queryset, use_distinct

Django: 1.11.5

Python: 3.6.2

最佳答案

我从您的问题中了解到,您希望 search_fields 和 _unaccent 搜索一起工作。

我不确定我是否正确理解了您的查询。

这是我所理解的解决方案:

def get_search_results(self, request, queryset, search_term):  # TODO: Make this more professionally implemented (proper overrides)
""" Overrides default search completely to incorporate __search and __unaccent lookups """
queryset1, use_distinct = super(<Admin class>, self).get_search_results(request, queryset, search_term)

queryset2 = queryset
if search_term: # Note: "if" condition necessary to show ALL results in admin if not search_term is specified (otherwise shows 0 results)
queryset2 = queryset.annotate(unaccent_title=SearchVector(*self.search_fields, config='english_unaccent')).filter(unaccent_title=SearchQuery(search_term, config='english_unaccent'))

return queryset1 | queryset2, use_distinct

如果您想要其他东西,请告诉我。

关于python - 管理中的 Django _unaccent 和 _search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com