#3937 closed (fixed)
UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | other branch |
Severity: | Keywords: | unicode str unicode-branch | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
When i try to use objects.filter this way:
Participante.objects.filter(nomecontains=u"á")
it returns this exception:
Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 102, in __repr__ return repr(self._get_data()) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 470, in _get_data self._result_cache = list(self.iterator()) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 174, in iterator select, sql, params = self._get_sql_clause() File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 484, in _get_sql_clause joins2, where2, params2 = self._filters.get_sql(opts) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 648, in get_sql joins2, where2, params2 = val.get_sql(opts) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 699, in get_sql return parse_lookup(self.kwargs.items(), opts) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 831, in parse_lookup joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 1009, in lookup_inner params.extend(field.get_db_prep_lookup(lookup_type, value)) File "/usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py", line 173, in get_db_prep_lookup return ["%%%s%%" % prep_for_like_query(value)] File "/usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py", line 25, in <lambda> prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)
I don't know if it is a bug, BUT me and some guys from django-brasil have tried a lot of ways before changing django code, so...
Changing str() for unicode() in file django/db/models/fields/init.py line 25, solved the problem.
the patch (svn diff):
Index: __init__.py # prepares a value for use in a LIKE query -prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") +prep_for_like_query = lambda x: unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
Sorry for my english... i'm from Brazil :)
Thanks,
Bruno Gola
Change History (4)
comment:1 by , 18 years ago
Patch needs improvement: | set |
---|---|
Summary: | Small bug when trying to use the filter method with LIKE operator and unicode strings → [unicode] UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings |
Triage Stage: | Unreviewed → Accepted |
Version: | SVN → other branch |
comment:2 by , 18 years ago
Keywords: | unicode-branch added |
---|---|
Summary: | [unicode] UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings → UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings |
comment:3 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
This is fixed on the unicode branch in [5203] (there's even a test for it now). I'll mark this ticket closed when that branch is merged back into trunk.