Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#3387 closed (worksforme)

UnicodeEncodeError on contains and icontains

Reported by: Gacha Owned by: adrian
Component: Core (Other) Version: master
Severity: Keywords: UnicodeEncodeError
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:


I got UnicodeEncodeError when I try to search with "icontains" and unicode string.

In [26]: Product.objects.filter(name=unicode('Fantasia - ķiršu','utf-8'))
Out[26]: [<Product: Fantasia - ķiršu>]

In [27]: Product.objects.filter(name__icontains=unicode('Fantasia - ķiršu','utf-8'))
exceptions.UnicodeEncodeError                        Traceback (most recent call last)
/home/***/django/db/models/fields/ in <lambda>(x)
     24 # prepares a value for use in a LIKE query
---> 25 prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
     27 # returns the <ul> class for a given radio_admin value

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0137' in position 11: ordinal not in range(128)

Attachments (1)

correction.diff (216 bytes) - added by Gacha 9 years ago.

Download all attachments as: .zip

Change History (5)

Changed 9 years ago by Gacha

comment:1 Changed 9 years ago by Gacha

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set
  • Resolution set to worksforme
  • Status changed from new to closed

Patch replaces the str() function with unicode() function in django/db/models/fields/ at 25 line.

comment:2 Changed 9 years ago by Michael Radziej <mir@…>

Gaicha, currently you should not use unicode strings within the database API. It is planned to support this in the future, but as long as this has not happened, you'll run into all sorts of problems. Better use utf8-encoded bytestrings.

comment:3 Changed 9 years ago by Michael Radziej <mir@…>

Sorry for misspelling your name, Gacha.

comment:4 Changed 9 years ago by Michael Radziej <mir@…>

  • Needs tests set

This is a duplicate of #2489, our general unicodification ticket ...

Note: See TracTickets for help on using tickets.
Back to Top