diff --git a/django/db/models/base.py b/django/db/models/base.py
index de47730403..645ee7eb95 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -12,6 +12,7 @@ from django.db.models.loading import register_models, get_model
from django.dispatch import dispatcher
from django.utils.datastructures import SortedDict
from django.utils.functional import curry
+from django.utils.encoding import smart_str
from django.conf import settings
from itertools import izip
import types
@@ -83,7 +84,7 @@ class Model(object):
return getattr(self, self._meta.pk.attname)
def __repr__(self):
- return '<%s: %s>' % (self.__class__.__name__, self)
+ return smart_str(u'<%s: %s>' % (self.__class__.__name__, self))
def __str__(self):
if hasattr(self, '__unicode__'):
@@ -326,7 +327,7 @@ class Model(object):
where = '(%s %s %%s OR (%s = %%s AND %s.%s %s %%s))' % \
(backend.quote_name(field.column), op, backend.quote_name(field.column),
backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column), op)
- param = str(getattr(self, field.attname))
+ param = smart_str(getattr(self, field.attname))
q = self.__class__._default_manager.filter(**kwargs).order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name)
q._where.append(where)
q._params.extend([param, param, getattr(self, self._meta.pk.attname)])
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 350ffc99f5..aa3596403e 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -9,6 +9,7 @@ from django.utils.functional import curry
from django.utils.itercompat import tee
from django.utils.text import capfirst
from django.utils.translation import ugettext, ugettext_lazy
+from django.utils.encoding import smart_unicode
import datetime, os, time
class NOT_PROVIDED:
@@ -22,7 +23,7 @@ BLANK_CHOICE_DASH = [("", "---------")]
BLANK_CHOICE_NONE = [("", "None")]
# 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: smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
# returns the
class for a given radio_admin value
get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
@@ -299,9 +300,9 @@ class Field(object):
return first_choice + list(self.choices)
rel_model = self.rel.to
if hasattr(self.rel, 'get_related_field'):
- lst = [(getattr(x, self.rel.get_related_field().attname), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
+ lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
else:
- lst = [(x._get_pk_val(), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
+ lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
return first_choice + lst
def get_choices_default(self):
@@ -423,7 +424,7 @@ class CharField(Field):
return value
else:
raise validators.ValidationError, ugettext_lazy("This field cannot be null.")
- return str(value)
+ return smart_unicode(value)
def formfield(self, **kwargs):
defaults = {'max_length': self.maxlength}
@@ -460,11 +461,11 @@ class DateField(Field):
def get_db_prep_lookup(self, lookup_type, value):
if lookup_type == 'range':
- value = [str(v) for v in value]
+ value = [smart_unicode(v) for v in value]
elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte') and hasattr(value, 'strftime'):
value = value.strftime('%Y-%m-%d')
else:
- value = str(value)
+ value = smart_unicode(value)
return Field.get_db_prep_lookup(self, lookup_type, value)
def pre_save(self, model_instance, add):
@@ -534,14 +535,14 @@ class DateTimeField(DateField):
# doesn't support microseconds.
if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
value = value.replace(microsecond=0)
- value = str(value)
+ value = smart_unicode(value)
return Field.get_db_prep_save(self, value)
def get_db_prep_lookup(self, lookup_type, value):
if lookup_type == 'range':
- value = [str(v) for v in value]
+ value = [smart_unicode(v) for v in value]
else:
- value = str(value)
+ value = smart_unicode(value)
return Field.get_db_prep_lookup(self, lookup_type, value)
def get_manipulator_field_objs(self):
@@ -811,9 +812,9 @@ class TimeField(Field):
def get_db_prep_lookup(self, lookup_type, value):
if lookup_type == 'range':
- value = [str(v) for v in value]
+ value = [smart_unicode(v) for v in value]
else:
- value = str(value)
+ value = smart_unicode(value)
return Field.get_db_prep_lookup(self, lookup_type, value)
def pre_save(self, model_instance, add):
@@ -831,7 +832,7 @@ class TimeField(Field):
# doesn't support microseconds.
if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
value = value.replace(microsecond=0)
- value = str(value)
+ value = smart_unicode(value)
return Field.get_db_prep_save(self, value)
def get_manipulator_field_objs(self):
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 0739d0461a..deb4535f18 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -5,6 +5,7 @@ from django.db.models.related import RelatedObject
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy, string_concat, ngettext
from django.utils.functional import curry
+from django.utils.encoding import smart_unicode
from django.core import validators
from django import oldforms
from django import newforms as forms
@@ -699,7 +700,7 @@ class ManyToManyField(RelatedField, Field):
if obj:
instance_ids = [instance._get_pk_val() for instance in getattr(obj, self.name).all()]
if self.rel.raw_id_admin:
- new_data[self.name] = ",".join([str(id) for id in instance_ids])
+ new_data[self.name] = u",".join([smart_unicode(id) for id in instance_ids])
else:
new_data[self.name] = instance_ids
else:
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 08a7901d6f..666924eb79 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -3,6 +3,7 @@ from django.db.models.fields import DateField, FieldDoesNotExist
from django.db.models import signals, loading
from django.dispatch import dispatcher
from django.utils.datastructures import SortedDict
+from django.utils.encoding import smart_unicode
from django.contrib.contenttypes import generic
import operator
import re
@@ -48,7 +49,7 @@ def handle_legacy_orderlist(order_list):
return order_list
else:
import warnings
- new_order_list = [LEGACY_ORDERING_MAPPING[j.upper()].replace('_', str(i)) for i, j in order_list]
+ new_order_list = [LEGACY_ORDERING_MAPPING[j.upper()].replace('_', smart_unicode(i)) for i, j in order_list]
warnings.warn("%r ordering syntax is deprecated. Use %r instead." % (order_list, new_order_list), DeprecationWarning)
return new_order_list
diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py
index 474287154a..5cf3a7622a 100644
--- a/tests/modeltests/validation/models.py
+++ b/tests/modeltests/validation/models.py
@@ -88,7 +88,7 @@ u'Jose'
>>> p.validate()
{}
>>> p.name
-'227'
+u'227'
>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
>>> p.validate()
diff --git a/tests/regressiontests/string_lookup/models.py b/tests/regressiontests/string_lookup/models.py
index 441bb3f8a3..658279a068 100644
--- a/tests/regressiontests/string_lookup/models.py
+++ b/tests/regressiontests/string_lookup/models.py
@@ -1,9 +1,11 @@
+# -*- coding: utf-8 -*-
from django.db import models
class Foo(models.Model):
name = models.CharField(maxlength=50)
+ viking = models.CharField(maxlength=50, blank=True)
- def __str__(self):
+ def __unicode__(self):
return "Foo %s" % self.name
class Bar(models.Model):
@@ -12,35 +14,35 @@ class Bar(models.Model):
fwd = models.ForeignKey("Whiz")
back = models.ForeignKey("Foo")
- def __str__(self):
+ def __unicode__(self):
return "Bar %s" % self.place.name
class Whiz(models.Model):
name = models.CharField(maxlength = 50)
- def __str__(self):
+ def __unicode__(self):
return "Whiz %s" % self.name
class Child(models.Model):
parent = models.OneToOneField('Base')
name = models.CharField(maxlength = 50)
- def __str__(self):
+ def __unicode__(self):
return "Child %s" % self.name
-
+
class Base(models.Model):
name = models.CharField(maxlength = 50)
- def __str__(self):
+ def __unicode__(self):
return "Base %s" % self.name
-__test__ = {'API_TESTS':"""
-# Regression test for #1661 and #1662: Check that string form referencing of models works,
-# both as pre and post reference, on all RelatedField types.
+__test__ = {'API_TESTS': ur"""
+# Regression test for #1661 and #1662: Check that string form referencing of
+# models works, both as pre and post reference, on all RelatedField types.
>>> f1 = Foo(name="Foo1")
>>> f1.save()
->>> f2 = Foo(name="Foo1")
+>>> f2 = Foo(name="Foo2")
>>> f2.save()
>>> w1 = Whiz(name="Whiz1")
@@ -56,7 +58,7 @@ __test__ = {'API_TESTS':"""
>>> b1.back
-
+
>>> base1 = Base(name="Base1")
>>> base1.save()
@@ -66,4 +68,12 @@ __test__ = {'API_TESTS':"""
>>> child1.parent
+
+# Regression tests for #3937: make sure we can use unicode characters in
+# queries.
+
+>>> fx = Foo(name='Bjorn', viking=u'Freydís Eiríksdóttir')
+>>> fx.save()
+>>> Foo.objects.get(viking__contains=u'\xf3')
+
"""}