Django

Code

Changeset 5386

Show
Ignore:
Timestamp:
05/30/07 23:25:40 (1 year ago)
Author:
mtredinnick
Message:

unicode: Changed all tests and documentation to use unicode instead of
str in places where it's appropriate to do so.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/unicode/docs/db-api.txt

    r5381 r5386  
    1616        tagline = models.TextField() 
    1717 
    18         def __str__(self): 
     18        def __unicode__(self): 
    1919            return self.name 
    2020 
     
    2323        email = models.URLField() 
    2424 
    25         def __str__(self): 
     25        def __unicode__(self): 
    2626            return self.name 
    2727 
     
    3333        authors = models.ManyToManyField(Author) 
    3434 
    35         def __str__(self): 
     35        def __unicode__(self): 
    3636            return self.headline 
    3737 
  • django/branches/unicode/docs/forms.txt

    r5310 r5386  
    4848            pass 
    4949 
    50         def __str__(self): 
     50        def __unicode__(self): 
    5151            return self.name 
    5252 
  • django/branches/unicode/docs/model-api.txt

    r5381 r5386  
    13401340 
    13411341 
    1342     * The ``__str__()`` method is just as valid in ``list_display`` as any 
    1343       other model method, so it's perfectly OK to do this:: 
    1344  
    1345           list_display = ('__str__', 'some_other_field') 
     1342    * The ``__str__()`` and ``__unicode__()`` methods are just as valid in 
     1343      ``list_display`` as any other model method, so it's perfectly OK to do 
     1344      this:: 
     1345 
     1346          list_display = ('__unicode__', 'some_other_field') 
    13461347 
    13471348    * Usually, elements of ``list_display`` that aren't actual database fields 
     
    17491750 
    17501751``__str__()`` is a Python "magic method" that defines what should be returned 
    1751 if you call ``str()`` on the object. Django uses ``str(obj)`` in a number of 
    1752 places, most notably as the value displayed to render an object in the Django 
    1753 admin site and as the value inserted into a template when it displays an 
    1754 object. Thus, you should always return a nice, human-readable string for the 
    1755 object's ``__str__``. Although this isn't required, it's strongly encouraged. 
     1752if you call ``str()`` on the object. Django uses ``str(obj)`` (or the related 
     1753function, ``unicode(obj)`` -- see below) in a number of places, most notably 
     1754as the value displayed to render an object in the Django admin site and as the 
     1755value inserted into a template when it displays an object. Thus, you should 
     1756always return a nice, human-readable string for the object's ``__str__``. 
     1757Although this isn't required, it's strongly encouraged (see the description of 
     1758``__unicode__``, below, before putting ``_str__`` methods everywhere). 
    17561759 
    17571760For example:: 
     
    17621765 
    17631766        def __str__(self): 
    1764             return '%s %s' % (self.first_name, self.last_name) 
     1767            # Note use of django.utils.encoding.smart_str() here because 
     1768            # first_name and last_name will be unicode strings. 
     1769            return smart_str('%s %s' % (self.first_name, self.last_name)) 
     1770 
     1771``__unicode__`` 
     1772--------------- 
     1773 
     1774The ``__unicode__()`` method is called whenever you call ``unicode()`` on an 
     1775object. Since Django's database backends will return Unicode strings in your 
     1776model's attributes, you would normally want to write a ``__unicode__()`` 
     1777method for your model. The example in the previous section could be written 
     1778more simply as:: 
     1779 
     1780    class Person(models.Model): 
     1781        first_name = models.CharField(maxlength=50) 
     1782        last_name = models.CharField(maxlength=50) 
     1783 
     1784        def __unicode__(self): 
     1785            return u'%s %s' % (self.first_name, self.last_name) 
     1786 
     1787If you define a ``__unicode__()`` method on your model and not a ``__str__()`` 
     1788method, Django will automatically provide you with a ``__str__()`` that calls 
     1789``__unicode()__`` and then converts the result correctly to a UTF-8 encoded 
     1790string object. This is recommended development practice: define only 
     1791``__unicode__()`` and let Django take care of the conversion to string objects 
     1792when required. 
    17651793 
    17661794``get_absolute_url`` 
  • django/branches/unicode/docs/newforms.txt

    r5381 r5386  
    13341334        birth_date = models.DateField(blank=True, null=True) 
    13351335 
    1336         def __str__(self): 
     1336        def __unicode__(self): 
    13371337            return self.name 
    13381338 
  • django/branches/unicode/docs/overview.txt

    r5054 r5386  
    2828        full_name = models.CharField(maxlength=70) 
    2929 
    30         def __str__(self): 
     30        def __unicode__(self): 
    3131            return self.full_name 
    3232 
     
    3737        reporter = models.ForeignKey(Reporter) 
    3838 
    39         def __str__(self): 
     39        def __unicode__(self): 
    4040            return self.headline 
    4141 
  • django/branches/unicode/docs/tutorial01.txt

    r5081 r5386  
    475475Wait a minute. ``<Poll: Poll object>`` is, utterly, an unhelpful 
    476476representation of this object. Let's fix that by editing the polls model (in 
    477 the ``polls/models.py`` file) and adding a ``__str__()`` method to both 
     477the ``polls/models.py`` file) and adding a ``__unicode__()`` method to both 
    478478``Poll`` and ``Choice``:: 
    479479 
    480480    class Poll(models.Model): 
    481481        # ... 
    482         def __str__(self): 
     482        def __unicode__(self): 
    483483            return self.question 
    484484 
    485485    class Choice(models.Model): 
    486486        # ... 
    487         def __str__(self): 
     487        def __unicode__(self): 
    488488            return self.choice 
    489489 
    490 It's important to add ``__str__()`` methods to your models, not only for your 
    491 own sanity when dealing with the interactive prompt, but also because objects' 
    492 representations are used throughout Django's automatically-generated admin. 
     490It's important to add ``__unicode__()`` methods to your models, not only for 
     491your own sanity when dealing with the interactive prompt, but also because 
     492objects' representations are used throughout Django's automatically-generated 
     493admin. 
     494 
     495.. admonition:: Why ``__unicode__`` and not ``__str__``? 
     496 
     497    If you are wondering why we add a ``__unicode__()`` method, rather than a 
     498    simple ``__str__()`` method, it is because Django models will contain 
     499    unicode strings by default. The values returned from the database, for 
     500    example, are all unicode strings. In most cases, your code should be 
     501    prepared to handle non-ASCII characters and this is a litle fiddly in 
     502    ``__str__()`` methods, since you have to worry about which encoding to 
     503    use, amongst other things. If you create a ``__unicode__()`` method, 
     504    Django will provide a ``__str__()`` method that calls your 
     505    ``__unicode__()`` and then converts the result to UTF-8 strings when 
     506    required. So ``unicode(p)`` will return a unicode string and ``str(p)`` 
     507    will return a normal string, with the characters encoded as UTF-8 when 
     508    necessary.. 
    493509 
    494510Note these are normal Python methods. Let's add a custom method, just for 
     
    510526    >>> from mysite.polls.models import Poll, Choice 
    511527 
    512     # Make sure our __str__() addition worked. 
     528    # Make sure our __unicode__() addition worked. 
    513529    >>> Poll.objects.all() 
    514530    [<Poll: What's up?>] 
  • django/branches/unicode/tests/modeltests/basic/models.py

    r5372 r5386  
    1515        ordering = ('pub_date','headline') 
    1616 
    17     def __str__(self): 
     17    def __unicode__(self): 
    1818        return self.headline 
    1919 
  • django/branches/unicode/tests/modeltests/choices/models.py

    r5320 r5386  
    2121    gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) 
    2222 
    23     def __str__(self): 
     23    def __unicode__(self): 
    2424        return self.name 
    2525 
  • django/branches/unicode/tests/modeltests/custom_columns/models.py

    r5185 r5386  
    2222    last_name = models.CharField(maxlength=30, db_column='last') 
    2323 
    24     def __str__(self): 
    25         return '%s %s' % (self.first_name, self.last_name) 
     24    def __unicode__(self): 
     25        return u'%s %s' % (self.first_name, self.last_name) 
    2626 
    2727    class Meta: 
     
    3333    authors = models.ManyToManyField(Author, db_table='my_m2m_table') 
    3434 
    35     def __str__(self): 
     35    def __unicode__(self): 
    3636        return self.headline 
    3737 
  • django/branches/unicode/tests/modeltests/custom_managers/models.py

    r3661 r5386  
    2424    objects = PersonManager() 
    2525 
    26     def __str__(self): 
    27         return "%s %s" % (self.first_name, self.last_name) 
     26    def __unicode__(self): 
     27        return u"%s %s" % (self.first_name, self.last_name) 
    2828 
    2929# An example of a custom manager that sets get_query_set(). 
     
    4040    authors = models.ManyToManyField(Person, related_name='books') 
    4141 
    42     def __str__(self): 
     42    def __unicode__(self): 
    4343        return self.title 
    4444 
     
    5656    fast_cars = FastCarManager() 
    5757 
    58     def __str__(self): 
     58    def __unicode__(self): 
    5959        return self.name 
    6060 
  • django/branches/unicode/tests/modeltests/custom_methods/models.py

    r3661 r5386  
    1212    pub_date = models.DateField() 
    1313 
    14     def __str__(self): 
     14    def __unicode__(self): 
    1515        return self.headline 
    1616 
  • django/branches/unicode/tests/modeltests/custom_pk/models.py

    r4971 r5386  
    1616        ordering = ('last_name', 'first_name') 
    1717 
    18     def __str__(self): 
    19         return "%s %s" % (self.first_name, self.last_name) 
     18    def __unicode__(self): 
     19        return u"%s %s" % (self.first_name, self.last_name) 
    2020 
    2121class Business(models.Model): 
     
    2525        verbose_name_plural = 'businesses' 
    2626 
    27     def __str__(self): 
     27    def __unicode__(self): 
    2828        return self.name 
    2929 
  • django/branches/unicode/tests/modeltests/field_defaults/models.py

    r4796 r5386  
    1717    pub_date = models.DateTimeField(default=datetime.now) 
    1818 
    19     def __str__(self): 
     19    def __unicode__(self): 
    2020        return self.headline 
    2121 
  • django/branches/unicode/tests/modeltests/fixtures/models.py

    r4971 r5386  
    1515    pub_date = models.DateTimeField() 
    1616 
    17     def __str__(self): 
     17    def __unicode__(self): 
    1818        return self.headline 
    1919 
  • django/branches/unicode/tests/modeltests/generic_relations/models.py

    r5185 r5386  
    2525        ordering = ["tag"] 
    2626     
    27     def __str__(self): 
     27    def __unicode__(self): 
    2828        return self.tag 
    2929 
     
    3434    tags = generic.GenericRelation(TaggedItem) 
    3535 
    36     def __str__(self): 
     36    def __unicode__(self): 
    3737        return self.common_name 
    3838         
     
    4343    tags = generic.GenericRelation(TaggedItem) 
    4444     
    45     def __str__(self): 
     45    def __unicode__(self): 
    4646        return self.name 
    4747     
     
    5252    # note the lack of an explicit GenericRelation here... 
    5353     
    54     def __str__(self): 
     54    def __unicode__(self): 
    5555        return self.name 
    5656         
  • django/branches/unicode/tests/modeltests/get_latest/models.py

    r3683 r5386  
    1818        get_latest_by = 'pub_date' 
    1919 
    20     def __str__(self): 
     20    def __unicode__(self): 
    2121        return self.headline 
    2222 
     
    2727    # Note that this model doesn't have "get_latest_by" set. 
    2828 
    29     def __str__(self): 
     29    def __unicode__(self): 
    3030        return self.name 
    3131 
  • django/branches/unicode/tests/modeltests/get_object_or_404/models.py

    r4796 r5386  
    1818    name = models.CharField(maxlength=50) 
    1919     
    20     def __str__(self): 
     20    def __unicode__(self): 
    2121        return self.name 
    2222 
     
    3131    by_a_sir = ArticleManager() 
    3232     
    33     def __str__(self): 
     33    def __unicode__(self): 
    3434        return self.title 
    3535 
  • django/branches/unicode/tests/modeltests/get_or_create/models.py

    r4796 r5386  
    1313    birthday = models.DateField() 
    1414 
    15     def __str__(self): 
    16         return '%s %s' % (self.first_name, self.last_name) 
     15    def __unicode__(self): 
     16        return u'%s %s' % (self.first_name, self.last_name) 
    1717 
    1818__test__ = {'API_TESTS':""" 
  • django/branches/unicode/tests/modeltests/lookup/models.py

    r5185 r5386  
    1313        ordering = ('-pub_date', 'headline') 
    1414 
    15     def __str__(self): 
     15    def __unicode__(self): 
    1616        return self.headline 
    1717 
  • django/branches/unicode/tests/modeltests/m2m_and_m2o/models.py

    r4796 r5386  
    1515    client = models.ForeignKey(User, related_name='test_issue_client') 
    1616 
    17     def __str__(self): 
    18         return str(self.num) 
     17    def __unicode__(self): 
     18        return unicode(self.num) 
    1919 
    2020    class Meta: 
  • django/branches/unicode/tests/modeltests/m2m_intermediary/models.py

    r3661 r5386  
    1717    last_name = models.CharField(maxlength=30) 
    1818 
    19     def __str__(self): 
    20         return "%s %s" % (self.first_name, self.last_name) 
     19    def __unicode__(self): 
     20        return u"%s %s" % (self.first_name, self.last_name) 
    2121 
    2222class Article(models.Model): 
     
    2424    pub_date = models.DateField() 
    2525 
    26     def __str__(self): 
     26    def __unicode__(self): 
    2727        return self.headline 
    2828 
     
    3232    position = models.CharField(maxlength=100) 
    3333 
    34     def __str__(self): 
    35         return '%s (%s)' % (self.reporter, self.position) 
     34    def __unicode__(self): 
     35        return u'%s (%s)' % (self.reporter, self.position) 
    3636 
    3737__test__ = {'API_TESTS':""" 
  • django/branches/unicode/tests/modeltests/m2m_multiple/models.py

    r3661 r5386  
    1515       ordering = ('name',) 
    1616 
    17     def __str__(self): 
     17    def __unicode__(self): 
    1818        return self.name 
    1919 
     
    2626       ordering = ('pub_date',) 
    2727 
    28     def __str__(self): 
     28    def __unicode__(self): 
    2929        return self.headline 
    3030 
  • django/branches/unicode/tests/modeltests/m2m_recursive/models.py

    r4796 r5386  
    2020    idols = models.ManyToManyField('self', symmetrical=False, related_name='stalkers') 
    2121 
    22     def __str__(self): 
     22    def __unicode__(self): 
    2323        return self.name 
    2424 
  • django/branches/unicode/tests/modeltests/m2o_recursive2/models.py

    r3661 r5386  
    1515    father = models.ForeignKey('self', null=True, related_name='fathers_child_set') 
    1616 
    17     def __str__(self): 
     17    def __unicode__(self): 
    1818        return self.full_name 
    1919 
  • django/branches/unicode/tests/modeltests/m2o_recursive/models.py

    r3661 r5386  
    1717    parent = models.ForeignKey('self', null=True, related_name='child_set') 
    1818 
    19     def __str__(self): 
     19    def __unicode__(self): 
    2020        return self.name 
    2121 
  • django/branches/unicode/tests/modeltests/manipulators/models.py

    r5082 r5386  
    1111    last_name = models.CharField(maxlength=30) 
    1212 
    13     def __str__(self): 
    14         return "%s %s" % (self.first_name, self.last_name) 
     13    def __unicode__(self): 
     14        return u"%s %s" % (self.first_name, self.last_name) 
    1515 
    1616class Album(models.Model): 
     
    1919    release_date = models.DateField(blank=True, null=True) 
    2020 
    21     def __str__(self): 
     21    def __unicode__(self): 
    2222        return self.name 
    2323 
  • django/branches/unicode/tests/modeltests/many_to_many/models.py

    r4448 r5386  
    1313    title = models.CharField(maxlength=30) 
    1414 
    15     def __str__(self): 
     15    def __unicode__(self): 
    1616        return self.title 
    1717 
     
    2323    publications = models.ManyToManyField(Publication) 
    2424 
    25     def __str__(self): 
     25    def __unicode__(self): 
    2626        return self.headline 
    2727 
  • django/branches/unicode/tests/modeltests/many_to_one/models.py

    r5243 r5386  
    1212    email = models.EmailField() 
    1313 
    14     def __str__(self): 
    15         return "%s %s" % (self.first_name, self.last_name) 
     14    def __unicode__(self): 
     15        return u"%s %s" % (self.first_name, self.last_name) 
    1616 
    1717class Article(models.Model): 
     
    2020    reporter = models.ForeignKey(Reporter) 
    2121 
    22     def __str__(self): 
     22    def __unicode__(self): 
    2323        return self.headline 
    2424 
  • django/branches/unicode/tests/modeltests/many_to_one_null/models.py

    r3661 r5386  
    1111    name = models.CharField(maxlength=30) 
    1212 
    13     def __str__(self): 
     13    def __unicode__(self): 
    1414        return self.name 
    1515 
     
    2121        ordering = ('headline',) 
    2222 
    23     def __str__(self): 
     23    def __unicode__(self): 
    2424        return self.headline 
    2525 
  • django/branches/unicode/tests/modeltests/model_forms/models.py

    r5241 r5386  
    3535    url = models.CharField('The URL', maxlength=40) 
    3636 
    37     def __str__(self): 
     37    def __unicode__(self): 
    3838        return self.name 
    3939 
     
    4141    name = models.CharField(maxlength=50, help_text='Use both first and last names.') 
    4242 
    43     def __str__(self): 
     43    def __unicode__(self): 
    4444        return self.name 
    4545 
     
    5959        return super(Article, self).save() 
    6060 
    61     def __str__(self): 
     61    def __unicode__(self): 
    6262        return self.headline 
    6363 
     
    6666    description = models.CharField(maxlength=20) 
    6767 
    68     def __str__(self): 
     68    def __unicode__(self): 
    6969        return self.phone 
    7070 
  • django/branches/unicode/tests/modeltests/model_inheritance/models.py

    r3661 r5386  
    1111    address = models.CharField(maxlength=80) 
    1212 
    13     def __str__(self): 
    14         return "%s the place" % self.name 
     13    def __unicode__(self): 
     14        return u"%s the place" % self.name 
    1515 
    1616class Restaurant(Place): 
     
    1818    serves_pizza = models.BooleanField() 
    1919 
    20     def __str__(self): 
    21         return "%s the restaurant" % self.name 
     20    def __unicode__(self): 
     21        return u"%s the restaurant" % self.name 
    2222 
    2323class ItalianRestaurant(Restaurant): 
    2424    serves_gnocchi = models.BooleanField() 
    2525 
    26     def __str__(self): 
    27         return "%s the italian restaurant" % self.name 
     26    def __unicode__(self): 
     27        return u"%s the italian restaurant" % self.name 
    2828 
    2929__test__ = {'API_TESTS':""" 
  • django/branches/unicode/tests/modeltests/one_to_one/models.py

    r3846 r5386  
    1313    address = models.CharField(maxlength=80) 
    1414 
    15     def __str__(self): 
    16         return "%s the place" % self.name 
     15    def __unicode__(self): 
     16        return u"%s the place" % self.name 
    1717 
    1818class Restaurant(models.Model): 
     
    2121    serves_pizza = models.BooleanField() 
    2222 
    23     def __str__(self): 
    24         return "%s the restaurant" % self.place.name 
     23    def __unicode__(self): 
     24        return u"%s the restaurant" % self.place.name 
    2525 
    2626class Waiter(models.Model): 
     
    2828    name = models.CharField(maxlength=50) 
    2929 
    30     def __str__(self): 
    31         return "%s the waiter at %s" % (self.name, self.restaurant) 
     30    def __unicode__(self): 
     31        return u"%s the waiter at %s" % (self.name, self.restaurant) 
    3232 
    3333class ManualPrimaryKey(models.Model): 
  • django/branches/unicode/tests/modeltests/ordering/models.py

    r3661 r5386  
    2222        ordering = ('-pub_date', 'headline') 
    2323 
    24     def __str__(self): 
     24    def __unicode__(self): 
    2525        return self.headline 
    2626 
  • django/branches/unicode/tests/modeltests/or_lookups/models.py

    r4971 r5386  
    2121       ordering = ('pub_date',) 
    2222 
    23     def __str__(self): 
     23    def __unicode__(self): 
    2424        return self.headline 
    2525 
  • django/branches/unicode/tests/modeltests/pagination/models.py

    r4796 r5386  
    1313    pub_date = models.DateTimeField() 
    1414 
    15     def __str__(self): 
     15    def __unicode__(self): 
    1616        return self.headline 
    1717 
  • django/branches/unicode/tests/modeltests/reserved_names/models.py

    r3661 r5386  
    2222       db_table = 'select' 
    2323 
    24     def __str__(self): 
     24    def __unicode__(self): 
    2525        return self.when 
    2626 
  • django/branches/unicode/tests/modeltests/reverse_lookup/models.py

    r5185 r5386  
    1010    name = models.CharField(maxlength=200) 
    1111 
    12     def __str__(self): 
     12    def __unicode__(self): 
    1313        return self.name 
    1414 
     
    1717    creator = models.ForeignKey(User) 
    1818 
    19     def __str__(self): 
     19    def __unicode__(self): 
    2020        return self.question 
    2121 
     
    2525    related_poll = models.ForeignKey(Poll, related_name="related_choice") 
    2626 
    27     def __str(self): 
     27    def __unicode__(self): 
    2828        return self.name 
    2929 
  • django/branches/unicode/tests/modeltests/save_delete_hooks/models.py

    r3661 r5386  
    1212    last_name = models.CharField(maxlength=20) 
    1313 
    14     def __str__(self): 
    15         return "%s %s" % (self.first_name, self.last_name) 
     14    def __unicode__(self): 
     15        return u"%s %s" % (self.first_name, self.last_name) 
    1616 
    1717    def save(self): 
  • django/branches/unicode/tests/modeltests/select_related/models.py

    r4797 r5386  
    1414class Domain(models.Model): 
    1515    name = models.CharField(maxlength=50) 
    16     def __str__(self): 
     16    def __unicode__(self): 
    1717        return self.name 
    1818 
     
    2020    name = models.CharField(maxlength=50) 
    2121    domain = models.ForeignKey(Domain) 
    22     def __str__(self): 
     22    def __unicode__(self): 
    2323        return self.name 
    2424 
     
    2626    name = models.CharField(maxlength=50) 
    2727    kingdom = models.ForeignKey(Kingdom) 
    28     def __str__(self): 
     28    def __unicode__(self): 
    2929        return self.name 
    3030     
     
    3232    name = models.CharField(maxlength=50) 
    3333    phylum = models.ForeignKey(Phylum) 
    34     def __str__(self): 
     34    def __unicode__(self): 
    3535        return self.name 
    3636     
     
    3838    name = models.CharField(maxlength=50) 
    3939    klass = models.ForeignKey(Klass) 
    40     def __str__(self): 
     40    def __unicode__(self): 
    4141        return self.name 
    4242 
     
    4444    name = models.CharField(maxlength=50) 
    4545    or