Django

Code

Changeset 5057

Show
Ignore:
Timestamp:
04/21/07 22:13:00 (1 year ago)
Author:
mtredinnick
Message:

unicode: Fixed #3996. Added check for model-specific unicode method to
default Model.str method. Thanks, Ivan Sagalaev.

Files:

Legend:

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

    r4893 r5057  
    8787 
    8888    def __str__(self): 
     89        if hasattr(self, '__unicode__'): 
     90            return unicode(self).encode('utf-8') 
    8991        return '%s object' % self.__class__.__name__ 
    9092 
  • django/branches/unicode/tests/modeltests/str/models.py

    r3661 r5057  
     1# -*- coding: utf-8 -*- 
    12""" 
    2 2. Adding __str__() to models 
     32. Adding __str__() or __unicode__() to models 
    34 
    45Although it's not a strict requirement, each model should have a ``__str__()`` 
     
    78because objects' representations are used throughout Django's 
    89automatically-generated admin. 
     10 
     11For international applications, you should write ``__unicode__``() method 
     12instead. 
    913""" 
    1014 
     
    1822        return self.headline 
    1923 
    20 __test__ = {'API_TESTS':""" 
     24class InternationalArticle(models.Model): 
     25    headline = models.CharField(maxlength=100) 
     26    pub_date = models.DateTimeField() 
     27 
     28    def __unicode__(self): 
     29        return self.headline 
     30 
     31__test__ = {'API_TESTS':ur""" 
    2132# Create an Article. 
    2233>>> from datetime import datetime 
     
    2940>>> a 
    3041<Article: Area man programs in Python> 
     42 
     43>>> a1 = InternationalArticle(headline=u'Girl wins €12.500 in lottery', pub_date=datetime(2005, 7, 28)) 
     44 
     45# The default str() output will be the UTF-8 encoded output of __unicode__(). 
     46>>> str(a1) 
     47'Girl wins \xe2\x82\xac12.500 in lottery' 
    3148"""}