[py3] Refactored __unicode__ to __str__.

* Renamed the __unicode__ methods
* Applied the python_2_unicode_compatible decorator
* Removed the StrAndUnicode mix-in that is superseded by
  python_2_unicode_compatible
* Kept the __unicode__ methods in classes that specifically
  test it under Python 2
This commit is contained in:
Aymeric Augustin 2012-08-12 12:32:08 +02:00
parent 79d62a7175
commit d4a0b27838
142 changed files with 1072 additions and 481 deletions

View file

@ -14,11 +14,13 @@ Both styles are demonstrated here.
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
#
# Abstract base classes
#
@python_2_unicode_compatible
class CommonInfo(models.Model):
name = models.CharField(max_length=50)
age = models.PositiveIntegerField()
@ -27,7 +29,7 @@ class CommonInfo(models.Model):
abstract = True
ordering = ['name']
def __unicode__(self):
def __str__(self):
return '%s %s' % (self.__class__.__name__, self.name)
class Worker(CommonInfo):
@ -49,6 +51,7 @@ class StudentWorker(Student, Worker):
class Post(models.Model):
title = models.CharField(max_length=50)
@python_2_unicode_compatible
class Attachment(models.Model):
post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
content = models.TextField()
@ -56,7 +59,7 @@ class Attachment(models.Model):
class Meta:
abstract = True
def __unicode__(self):
def __str__(self):
return self.content
class Comment(Attachment):
@ -69,17 +72,19 @@ class Link(Attachment):
# Multi-table inheritance
#
@python_2_unicode_compatible
class Chef(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
def __str__(self):
return "%s the chef" % self.name
@python_2_unicode_compatible
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
def __unicode__(self):
def __str__(self):
return "%s the place" % self.name
class Rating(models.Model):
@ -89,6 +94,7 @@ class Rating(models.Model):
abstract = True
ordering = ['-rating']
@python_2_unicode_compatible
class Restaurant(Place, Rating):
serves_hot_dogs = models.BooleanField()
serves_pizza = models.BooleanField()
@ -97,27 +103,30 @@ class Restaurant(Place, Rating):
class Meta(Rating.Meta):
db_table = 'my_restaurant'
def __unicode__(self):
def __str__(self):
return "%s the restaurant" % self.name
@python_2_unicode_compatible
class ItalianRestaurant(Restaurant):
serves_gnocchi = models.BooleanField()
def __unicode__(self):
def __str__(self):
return "%s the italian restaurant" % self.name
@python_2_unicode_compatible
class Supplier(Place):
customers = models.ManyToManyField(Restaurant, related_name='provider')
def __unicode__(self):
def __str__(self):
return "%s the supplier" % self.name
@python_2_unicode_compatible
class ParkingLot(Place):
# An explicit link to the parent (we can control the attribute name).
parent = models.OneToOneField(Place, primary_key=True, parent_link=True)
main_site = models.ForeignKey(Place, related_name='lot')
def __unicode__(self):
def __str__(self):
return "%s the parking lot" % self.name
#
@ -139,10 +148,11 @@ class NamedURL(models.Model):
class Meta:
abstract = True
@python_2_unicode_compatible
class Copy(NamedURL):
content = models.TextField()
def __unicode__(self):
def __str__(self):
return self.content
class Mixin(object):