Opened 11 years ago

Closed 11 years ago

#3412 closed (fixed)

Search unicode string in DB

Reported by: Sébastien ( khepin@… Owned by: Adrian Holovaty
Component: Forms Version: master
Severity: Keywords: unicode
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


I try to search records in my DB and the information I give contains non-ascii characters. I used __icontains for the search. This leads to a unicode error in django.

This is the View that is used to search into DB:

def search_entreprise(request):
    form = EntrepriseSearchForm()
    if request.method=='POST':
        new_data = request.POST.copy()
        form = EntrepriseSearchForm(new_data)
        if form.is_valid():
            data = form.clean_data
            entreprise_list = Entreprise.objects.all()
            if data['domaine']:
            if data['entrepriseMere']:
            if data['nom']:
            return render_to_response('stagesECL/entreprise_list.html',{'list':entreprise_list,})
    return render_to_response('stagesECL/entreprise_search.html',{'form':form,})

where in this case the only information given is the entreprise name (nom) which had the value "hé".

Here is the error I get:

UnicodeEncodeError at /entreprise/search/
'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)
Request Method: 	POST
Request URL: 	http://localhost/entreprise/search/
Exception Type: 	UnicodeEncodeError
Exception Value: 	'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)
Exception Location: 	c:\Python25\lib\site-packages\django\db\models\fields\ in , line 25

And finally this is the backtrace:

Traceback (most recent call last):
File "c:\Python25\lib\site-packages\django\template\" in render_node
  712. result = node.render(context)
File "c:\Python25\lib\site-packages\django\template\" in render
  100. len_values = len(values)
File "c:\Python25\lib\site-packages\django\db\models\" in __len__
  100. return len(self._get_data())
File "c:\Python25\lib\site-packages\django\db\models\" in _get_data
  430. self._result_cache = list(self.iterator())
File "c:\Python25\lib\site-packages\django\db\models\" in iterator
  171. select, sql, params = self._get_sql_clause()
File "c:\Python25\lib\site-packages\django\db\models\" in _get_sql_clause
  444. joins2, where2, params2 = self._filters.get_sql(opts)
File "c:\Python25\lib\site-packages\django\db\models\" in get_sql
  574. joins2, where2, params2 = val.get_sql(opts)
File "c:\Python25\lib\site-packages\django\db\models\" in get_sql
  622. return parse_lookup(self.kwargs.items(), opts)
File "c:\Python25\lib\site-packages\django\db\models\" in parse_lookup
  743. joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None)
File "c:\Python25\lib\site-packages\django\db\models\" in lookup_inner
  915. params.extend(field.get_db_prep_lookup(lookup_type, value))
File "c:\Python25\lib\site-packages\django\db\models\fields\" in get_db_prep_lookup
  172. return ["%%%s%%" % prep_for_like_query(value)]
File "c:\Python25\lib\site-packages\django\db\models\fields\" in
  25. prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")

  UnicodeEncodeError at /entreprise/search/
  'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)

Change History (2)

comment:1 Changed 11 years ago by Michael Radziej <mir@…>

Has patch: set
Keywords: unicode added
Needs tests: set
Triage Stage: UnreviewedAccepted

The patch in ticket #3387 should fix it. #3387 was closed because the bug was triggered *there* by incorrectly using unicode as parameter for Queryset.filter(). I didn't notice that it makes sense to pass unicode parameters in the way demonstrated above (sorry about that).

Related thread in django-users

I check 'has patch' since it does have one, it's just in #3387.

comment:2 Changed 11 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

Closing because #3387 is closed.

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