Author: mtredinnick
Date: 2007-05-12 10:07:17 -0500 (Sat, 12 May 2007)
New Revision: 5203
Modified:
django/branches/unicode/django/db/models/base.py
django/branches/unicode/django/db/models/fields/__init__.py
django/branches/unicode/django/db/models/fields/related.py
django/branches/unicode/django/db/models/query.py
django/branches/unicode/tests/modeltests/validation/models.py
django/branches/unicode/tests/regressiontests/string_lookup/models.py
Log:
unicode: Added some more unicode conversions in django.db.models.*.
Modified: django/branches/unicode/django/db/models/base.py
===================================================================
--- django/branches/unicode/django/db/models/base.py 2007-05-12 14:42:46 UTC
(rev 5202)
+++ django/branches/unicode/django/db/models/base.py 2007-05-12 15:07:17 UTC
(rev 5203)
@@ -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 @@
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 @@
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)])
Modified: django/branches/unicode/django/db/models/fields/__init__.py
===================================================================
--- django/branches/unicode/django/db/models/fields/__init__.py 2007-05-12
14:42:46 UTC (rev 5202)
+++ django/branches/unicode/django/db/models/fields/__init__.py 2007-05-12
15:07:17 UTC (rev 5203)
@@ -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 @@
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 @@
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 @@
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 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 @@
# 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 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 @@
# 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):
Modified: django/branches/unicode/django/db/models/fields/related.py
===================================================================
--- django/branches/unicode/django/db/models/fields/related.py 2007-05-12
14:42:46 UTC (rev 5202)
+++ django/branches/unicode/django/db/models/fields/related.py 2007-05-12
15:07:17 UTC (rev 5203)
@@ -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 @@
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:
Modified: django/branches/unicode/django/db/models/query.py
===================================================================
--- django/branches/unicode/django/db/models/query.py 2007-05-12 14:42:46 UTC
(rev 5202)
+++ django/branches/unicode/django/db/models/query.py 2007-05-12 15:07:17 UTC
(rev 5203)
@@ -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 @@
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
Modified: django/branches/unicode/tests/modeltests/validation/models.py
===================================================================
--- django/branches/unicode/tests/modeltests/validation/models.py
2007-05-12 14:42:46 UTC (rev 5202)
+++ django/branches/unicode/tests/modeltests/validation/models.py
2007-05-12 15:07:17 UTC (rev 5203)
@@ -88,7 +88,7 @@
>>> p.validate()
{}
>>> p.name
-'227'
+u'227'
>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
>>> p.validate()
Modified: django/branches/unicode/tests/regressiontests/string_lookup/models.py
===================================================================
--- django/branches/unicode/tests/regressiontests/string_lookup/models.py
2007-05-12 14:42:46 UTC (rev 5202)
+++ django/branches/unicode/tests/regressiontests/string_lookup/models.py
2007-05-12 15:07:17 UTC (rev 5203)
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
from django.db import models
class Foo(models.Model):
@@ -2,4 +3,5 @@
name = models.CharField(maxlength=50)
+ viking = models.CharField(maxlength=50, blank=True)
- def __str__(self):
+ def __unicode__(self):
return "Foo %s" % self.name
@@ -12,35 +14,35 @@
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 @@
<Whiz: Whiz Whiz1>
>>> b1.back
-<Foo: Foo Foo1>
+<Foo: Foo Foo2>
>>> base1 = Base(name="Base1")
>>> base1.save()
@@ -66,4 +68,12 @@
>>> 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>
"""}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---