Django

Code

Ticket #3937 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings

Reported by: brunogola@gmail.com Assigned to: adrian
Milestone: Component: Database wrapper
Version: other branch Keywords: unicode str unicode-branch
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 1

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

Attachments

Change History

04/29/07 22:41:49 changed by Simon G. <dev@simon.net.nz>

  • needs_better_patch set to 1.
  • summary changed from Small bug when trying to use the filter method with LIKE operator and unicode strings to [unicode] UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings.
  • version changed from SVN to other branch.
  • stage changed from Unreviewed to Accepted.

05/12/07 10:07:45 changed by mtredinnick

  • keywords changed from unicode str to unicode str unicode-branch.
  • summary changed from [unicode] UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings to UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings.

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.

07/04/07 07:11:05 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5609]) Merged Unicode branch into trunk (r4952:5608). This should be fully backwards compatible for all practical purposes.

Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702


Add/Change #3937 (UnicodeEncodeError exception when using the filter method with LIKE operator and unicode strings)




Change Properties
Action