Django Book 翻译
菜单>:
TOC
返回
原文:
.. table:: Table B-7. Operators Allowed in search_fields +--------+-------------------------------------------------------------------------------------------------+ |Operator|Meaning | +========+=================================================================================================+ |``^`` |Matches the beginning of the field. For example, if ``search_fields`` is set to ``['^first_name',| | |'^last_name']`` , and a user searches for ``john lennon`` , Django will do the equivalent of this| | |SQL ``WHERE`` clause: | | | | | |:: | | | | | | | | | WHERE (first_name ILIKE 'john%' OR last_name ILIKE 'john%') | | | AND (first_name ILIKE 'lennon%' OR last_name ILIKE 'lennon%') | | | | | |.. | | | | | | | | | | | |This query is more efficient than the normal ``'%john%'`` query, because the database only needs | | |to check the beginning of a columns data, rather than seeking through the entire columns data. | | |Plus, if the column has an index on it, some databases may be able to use the index for this | | |query, even though its a ``LIKE`` query. | +--------+-------------------------------------------------------------------------------------------------+ |``=`` |Matches exactly, case-insensitive. For example, if ``search_fields`` is set to ``['=first_name', | | |'=last_name']`` and a user searches for ``john lennon`` , Django will do the equivalent of this | | |SQL ``WHERE`` clause: | | | | | |:: | | | | | | | | | WHERE (first_name ILIKE 'john' OR last_name ILIKE 'john') | | | AND (first_name ILIKE 'lennon' OR last_name ILIKE 'lennon') | | | | | |.. | | | | | | | | | | | |Note that the query input is split by spaces, so, following this example, its currently not | | |possible to search for all records in which ``first_name`` is exactly ``'john winston'`` | | |(containing a space). | +--------+-------------------------------------------------------------------------------------------------+ |``@`` |Performs a full-text match. This is like the default search method, but it uses an index. | | |Currently this is available only for MySQL. | +--------+-------------------------------------------------------------------------------------------------+
翻译:
.. table:: 表B-7. search_fields中允许使用的操作符 +--------+-------------------------------------------------------------------------------------------------+ |操作符 |含义 | +========+=================================================================================================+ |``^`` |匹配字段的开头。例如,把 ``search_fields`` 设置成 ``['^first_name', '^last_name']`` ,当用户搜索 | | |``john lennon`` 时,Django相当于执行了这样的SQL ``WHERE`` 语句: | | | | | |:: | | | | | | | | | WHERE (first_name ILIKE 'john%' OR last_name ILIKE 'john%') | | | AND (first_name ILIKE 'lennon%' OR last_name ILIKE 'lennon%') | | | | | |.. | | | | | | | | | | | |这个查询要比执行普通的 ``'%john%'`` 查询效率高,因为数据库只需要检查每一列数据的开头,而不用把整 | | |列数据都扫一遍。此外,如果有针对这一列的索引的话,某些数据库可能会在查询中使用索引,即使它是一个 | | |``LIKE`` 查询。 | +--------+-------------------------------------------------------------------------------------------------+ |``=`` |精确匹配,不区分大小写。例如,把 ``search_fields`` 设置成 ``['=first_name', '=last_name']`` ,当 | | |用户搜索 ``john lennon`` 时,Django相当于执行了这样的SQL ``WHERE`` 语句: | | | | | |:: | | | | | | | | | WHERE (first_name ILIKE 'john' OR last_name ILIKE 'john') | | | AND (first_name ILIKE 'lennon' OR last_name ILIKE 'lennon') | | | | | |.. | | | | | | | | | | | |记住,搜索输入是靠空格来分隔的,所以,在这个例子中还不可能找出 ``first_name`` 恰恰是 | | |``'john winston'`` (中间有空格)的所有记录。 | +--------+-------------------------------------------------------------------------------------------------+ |``@`` |执行全文匹配。这个和默认的搜索方法类似,但是它使用索引。目前只在MySQL中可用。 | +--------+-------------------------------------------------------------------------------------------------+
备注:
译者: