fix queryset problem with non-ascii chars

This commit is contained in:
Itai Shirav 2017-05-05 15:39:01 +03:00
parent 00ca503b7c
commit 6301ab468e
3 changed files with 12 additions and 5 deletions

View File

@ -234,7 +234,7 @@ class Database(object):
return set(obj.module_name for obj in self.select(query))
def _send(self, data, settings=None, stream=False):
if PY3 and isinstance(data, string_types):
if isinstance(data, string_types):
data = data.encode('utf-8')
params = self._build_params(settings)
r = requests.post(self.db_url, params=params, data=data, stream=stream)

View File

@ -195,13 +195,13 @@ class QuerySet(object):
if self._fields:
fields = ', '.join('`%s`' % field for field in self._fields)
params = (fields, self._database.db_name, self._model_cls.table_name(), self.conditions_as_sql(), self.order_by_as_sql())
return 'SELECT %s\nFROM `%s`.`%s`\nWHERE %s\nORDER BY %s' % params
return u'SELECT %s\nFROM `%s`.`%s`\nWHERE %s\nORDER BY %s' % params
def order_by_as_sql(self):
"""
Returns the contents of the query's `ORDER BY` clause as a string.
"""
return ', '.join([
return u', '.join([
'%s DESC' % field[1:] if field[0] == '-' else field
for field in self._order_by
])
@ -211,9 +211,9 @@ class QuerySet(object):
Returns the contents of the query's `WHERE` clause as a string.
"""
if self._q:
return ' AND '.join([q.to_sql(self._model_cls) for q in self._q])
return u' AND '.join([q.to_sql(self._model_cls) for q in self._q])
else:
return '1'
return u'1'
def count(self):
"""

View File

@ -55,6 +55,13 @@ class QuerySetTestCase(TestCaseWithData):
self._test_qs(qs.filter(first_name__iendswith='ia'), 3) # case insensitive
self._test_qs(qs.filter(first_name__iendswith=''), 100) # empty suffix
def test_filter_unicode_string(self):
self.database.insert([
Person(first_name=u'דונלד', last_name=u'דאק')
])
qs = Person.objects_in(self.database)
self._test_qs(qs.filter(first_name=u'דונלד'), 1)
def test_filter_float_field(self):
qs = Person.objects_in(self.database)
self._test_qs(qs.filter(height__gt=2), 0)