django管理员搜索多个单词 [英] django admin search with multiple words
本文介绍了django管理员搜索多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当搜索表达式有多个单词时,我有麻烦使用 search_fields
,而我想执行 搜索
I have troubles using the search_fields
when the search expression has multiple words and i want to perform a startswith search
我有一个课程
class Foo(models.Model):
kw = models.CharField(max_length = 255)
...
class FooAdmin(admin.ModelAdmin):
search_fields = ('^kw',)
'^'
表示我想执行一个启动搜索。如果我正在寻找kw'foo fuu',django将执行查询:
The '^'
indicates that i want to perform a startswith search. If i'm looking for the kw 'foo fuu', django will perform the query:
select * from app_foo where `foo`.`kw` like 'foo%' and `foo`.`kw` like 'fuu%'
这个查询显然给出了零结果。我应该怎么做让引擎寻找'foo fuu%'
?
This query obviously gives zero results. What should i do to make the engine looking for 'foo fuu%'
?
推荐答案
如何覆盖 self.query
so split()
不起作用?
How about override self.query
so split()
doesn't work?
class UnsplitableUnicode(unicode):
"An object that behaves like a unicode string but cannot be split()"
def split(self, *args, **kwargs):
return [self]
class MultiWordSearchChangeList(ChangeList):
"Changelist that allows searches to contain spaces"
def get_query_set(self, request):
self.query = UnsplitableUnicode(self.query)
return super(MultiWordSearchChangeList, self).get_query_set(request)
class FooAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return MultiWordSearchChangeList
这篇关于django管理员搜索多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文