Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

unicode: Added some more unicode conversions in django.db.models.*.

git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5203 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 994714d8cc68701abb5190c1fd9d8c3015744797 1 parent 1026402
@malcolmt malcolmt authored
View
5 django/db/models/base.py
@@ -12,6 +12,7 @@
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 @@ def _get_pk_val(self):
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 @@ def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
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)])
View
25 django/db/models/fields/__init__.py
@@ -9,6 +9,7 @@
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 @@ class NOT_PROVIDED:
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 <ul> class for a given radio_admin value
get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
@@ -299,9 +300,9 @@ def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
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 @@ def to_python(self, value):
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 @@ def to_python(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]
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 @@ def get_db_prep_save(self, value):
# 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 @@ def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=Fa
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 @@ def get_db_prep_save(self, value):
# 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):
View
3  django/db/models/fields/related.py
@@ -5,6 +5,7 @@
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 @@ def flatten_data(self, follow, obj = None):
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:
View
3  django/db/models/query.py
@@ -3,6 +3,7 @@
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
View
2  tests/modeltests/validation/models.py
@@ -88,7 +88,7 @@ def __str__(self):
>>> p.validate()
{}
>>> p.name
-'227'
+u'227'
>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
>>> p.validate()
View
32 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 @@ def __str__(self):
<Whiz: Whiz Whiz1>
>>> b1.back
-<Foo: Foo Foo1>
+<Foo: Foo Foo2>
>>> base1 = Base(name="Base1")
>>> base1.save()
@@ -66,4 +68,12 @@ def __str__(self):
>>> child1.parent
<Base: Base Base1>
+
+# 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')
+<Foo: Foo Bjorn>
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.