Django strange icontains behaviour in development
I have been getting some strange behaviour using icontains
on my development server. I have a complex query which has been returning some strange results (not the expected number). I drilled down to find the problem seems to be with icontains
.
First off, here is the mysql output of part of the query I want to run in Django:
mysql> select * from species where en_name RLIKE 'tortoise';
+-------+----------------------------------+
| id | en_name |
+-------+----------------------------------+
| 16215 | Santa Cruz Galapagos Tortoise |
| 16214 | Floreana Galapagos Tortoise |
| 16213 | Volcan Darwin Galapagos Tortoise |
| 16211 | Sierra Negra Galapagos Tortoise |
| 16210 | Pinzon Galapagos Tortoise |
| 16209 | Santiago Galapagos Tortoise |
| 738 | River Tortoise |
| 16207 | Volcan Wolf Galapagos Tortoise |
| 16206 | Pinta Galapagos Tortoise |
| 16216 | Fernandina Galapagos Tortoise |
| 16217 | Santa Fe Galapagos Tortoise |
| 16218 | Alcedo Galapagos Tortoise |
| 16219 | Cerro Azul Galapagos Tortoise |
| 16220 | Rabida Galapagos Tortoise |
+-------+----------------------------------+
14 rows in set (0.00 sec)
When I drop into the Django shell and run:
query_set = Species.objects.filter(en_name__icontains='tortoise')
I get a single result:
>>> for i in query_set:
... print i.en_name
...
River Tortoise
At a guess I would say it is something to do with the number of whitespaces in the en_name
string (River Tortoise has one whereas the others have >1). I have also heard that icontains
can behave differently in development and production.
Can anyone shed some light on this?
如果你想做RLIKE,我的理解是REGEXP的别名http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp
query_set = Species.objects.filter(en_name__regex='tortoise')
链接地址: http://www.djcxy.com/p/6588.html
上一篇: 只在Django查询中匹配单词