1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2007-05-12 15:07:17 +00:00
parent 1026402499
commit 994714d8cc
6 changed files with 42 additions and 28 deletions

View File

@ -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)])

View File

@ -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 <ul> 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):

View File

@ -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:

View File

@ -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

View File

@ -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()

View File

@ -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':"""
<Whiz: Whiz Whiz1>
>>> b1.back
<Foo: Foo Foo1>
<Foo: Foo Foo2>
>>> base1 = Base(name="Base1")
>>> base1.save()
@ -66,4 +68,12 @@ __test__ = {'API_TESTS':"""
>>> 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>
"""}