Django

Code

Changeset 5203

Show
Ignore:
Timestamp:
05/12/07 10:07:17 (1 year ago)
Author:
mtredinnick
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/unicode/django/db/models/base.py

    r5185 r5203  
    1313from django.utils.datastructures import SortedDict 
    1414from django.utils.functional import curry 
     15from django.utils.encoding import smart_str 
    1516from django.conf import settings 
    1617from itertools import izip 
     
    8485 
    8586    def __repr__(self): 
    86         return '<%s: %s>' % (self.__class__.__name__, self
     87        return smart_str(u'<%s: %s>' % (self.__class__.__name__, self)
    8788 
    8889    def __str__(self): 
     
    327328            (backend.quote_name(field.column), op, backend.quote_name(field.column), 
    328329            backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column), op) 
    329         param = str(getattr(self, field.attname)) 
     330        param = smart_str(getattr(self, field.attname)) 
    330331        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) 
    331332        q._where.append(where) 
  • django/branches/unicode/django/db/models/fields/__init__.py

    r5151 r5203  
    1010from django.utils.text import capfirst 
    1111from django.utils.translation import ugettext, ugettext_lazy 
     12from django.utils.encoding import smart_unicode 
    1213import datetime, os, time 
    1314 
     
    2324 
    2425# prepares a value for use in a LIKE query 
    25 prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") 
     26prep_for_like_query = lambda x: smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") 
    2627 
    2728# returns the <ul> class for a given radio_admin value 
     
    300301        rel_model = self.rel.to 
    301302        if hasattr(self.rel, 'get_related_field'): 
    302             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)] 
    303         else: 
    304             lst = [(x._get_pk_val(), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)] 
     303            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)] 
     304        else: 
     305            lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)] 
    305306        return first_choice + lst 
    306307 
     
    424425            else: 
    425426                raise validators.ValidationError, ugettext_lazy("This field cannot be null.") 
    426         return str(value) 
     427        return smart_unicode(value) 
    427428 
    428429    def formfield(self, **kwargs): 
     
    461462    def get_db_prep_lookup(self, lookup_type, value): 
    462463        if lookup_type == 'range': 
    463             value = [str(v) for v in value] 
     464            value = [smart_unicode(v) for v in value] 
    464465        elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte') and hasattr(value, 'strftime'): 
    465466            value = value.strftime('%Y-%m-%d') 
    466467        else: 
    467             value = str(value) 
     468            value = smart_unicode(value) 
    468469        return Field.get_db_prep_lookup(self, lookup_type, value) 
    469470 
     
    535536            if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'): 
    536537                value = value.replace(microsecond=0) 
    537             value = str(value) 
     538            value = smart_unicode(value) 
    538539        return Field.get_db_prep_save(self, value) 
    539540 
    540541    def get_db_prep_lookup(self, lookup_type, value): 
    541542        if lookup_type == 'range': 
    542             value = [str(v) for v in value] 
    543         else: 
    544             value = str(value) 
     543            value = [smart_unicode(v) for v in value] 
     544        else: 
     545            value = smart_unicode(value) 
    545546        return Field.get_db_prep_lookup(self, lookup_type, value) 
    546547 
     
    812813    def get_db_prep_lookup(self, lookup_type, value): 
    813814        if lookup_type == 'range': 
    814             value = [str(v) for v in value] 
    815         else: 
    816             value = str(value) 
     815            value = [smart_unicode(v) for v in value] 
     816        else: 
     817            value = smart_unicode(value) 
    817818        return Field.get_db_prep_lookup(self, lookup_type, value) 
    818819 
     
    832833            if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'): 
    833834                value = value.replace(microsecond=0) 
    834             value = str(value) 
     835            value = smart_unicode(value) 
    835836        return Field.get_db_prep_save(self, value) 
    836837 
  • django/branches/unicode/django/db/models/fields/related.py

    r5126 r5203  
    66from django.utils.translation import gettext_lazy, string_concat, ngettext 
    77from django.utils.functional import curry 
     8from django.utils.encoding import smart_unicode 
    89from django.core import validators 
    910from django import oldforms 
     
    700701            instance_ids = [instance._get_pk_val() for instance in getattr(obj, self.name).all()] 
    701702            if self.rel.raw_id_admin: 
    702                 new_data[self.name] = ",".join([str(id) for id in instance_ids]) 
     703                new_data[self.name] = u",".join([smart_unicode(id) for id in instance_ids]) 
    703704            else: 
    704705                new_data[self.name] = instance_ids 
  • django/branches/unicode/django/db/models/query.py

    r5185 r5203  
    44from django.dispatch import dispatcher 
    55from django.utils.datastructures import SortedDict 
     6from django.utils.encoding import smart_unicode 
    67from django.contrib.contenttypes import generic 
    78import operator 
     
    4950    else: 
    5051        import warnings 
    51         new_order_list = [LEGACY_ORDERING_MAPPING[j.upper()].replace('_', str(i)) for i, j in order_list] 
     52        new_order_list = [LEGACY_ORDERING_MAPPING[j.upper()].replace('_', smart_unicode(i)) for i, j in order_list] 
    5253        warnings.warn("%r ordering syntax is deprecated. Use %r instead." % (order_list, new_order_list), DeprecationWarning) 
    5354        return new_order_list 
  • django/branches/unicode/tests/modeltests/validation/models.py

    r5082 r5203  
    8989{} 
    9090>>> p.name 
    91 '227' 
     91u'227' 
    9292 
    9393>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3))) 
  • django/branches/unicode/tests/regressiontests/string_lookup/models.py

    r3661 r5203  
     1# -*- coding: utf-8 -*- 
    12from django.db import models 
    23 
    34class Foo(models.Model): 
    45    name = models.CharField(maxlength=50) 
     6    viking = models.CharField(maxlength=50, blank=True) 
    57 
    6     def __str__(self): 
     8    def __unicode__(self): 
    79        return "Foo %s" % self.name 
    810 
     
    1315    back = models.ForeignKey("Foo") 
    1416 
    15     def __str__(self): 
     17    def __unicode__(self): 
    1618        return "Bar %s" % self.place.name 
    1719 
     
    1921    name = models.CharField(maxlength = 50) 
    2022 
    21     def __str__(self): 
     23    def __unicode__(self): 
    2224        return "Whiz %s" % self.name 
    2325 
     
    2628    name = models.CharField(maxlength = 50) 
    2729 
    28     def __str__(self): 
     30    def __unicode__(self): 
    2931        return "Child %s" % self.name 
    30      
     32 
    3133class Base(models.Model): 
    3234    name = models.CharField(maxlength = 50) 
    3335 
    34     def __str__(self): 
     36    def __unicode__(self): 
    3537        return "Base %s" % self.name 
    3638 
    37 __test__ = {'API_TESTS':""" 
    38 # Regression test for #1661 and #1662: Check that string form referencing of models works,  
    39 # both as pre and post reference, on all RelatedField types. 
     39__test__ = {'API_TESTS': ur""" 
     40# Regression test for #1661 and #1662: Check that string form referencing of 
     41# models works, both as pre and post reference, on all RelatedField types. 
    4042 
    4143>>> f1 = Foo(name="Foo1") 
    4244>>> f1.save() 
    43 >>> f2 = Foo(name="Foo1") 
     45>>> f2 = Foo(name="Foo2") 
    4446>>> f2.save() 
    4547 
     
    5759 
    5860>>> b1.back 
    59 <Foo: Foo Foo1
     61<Foo: Foo Foo2
    6062 
    6163>>> base1 = Base(name="Base1") 
     
    6769>>> child1.parent 
    6870<Base: Base Base1> 
     71 
     72# Regression tests for #3937: make sure we can use unicode characters in 
     73# queries. 
     74 
     75>>> fx = Foo(name='Bjorn', viking=u'Freydís Eiríksdóttir') 
     76>>> fx.save() 
     77>>> Foo.objects.get(viking__contains=u'\xf3') 
     78<Foo: Foo Bjorn> 
    6979"""}