mirror of
https://github.com/django/django.git
synced 2025-11-20 03:30:00 +00:00
[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:
parent
79d62a7175
commit
d4a0b27838
142 changed files with 1072 additions and 481 deletions
|
|
@ -4,20 +4,23 @@ import datetime
|
|||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class BetterAuthor(Author):
|
||||
write_speed = models.IntegerField()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
title = models.CharField(max_length=100)
|
||||
|
|
@ -28,20 +31,22 @@ class Book(models.Model):
|
|||
)
|
||||
ordering = ['id']
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class BookWithCustomPK(models.Model):
|
||||
my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
|
||||
author = models.ForeignKey(Author)
|
||||
title = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s: %s' % (self.my_pk, self.title)
|
||||
|
||||
class Editor(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class BookWithOptionalAltEditor(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
# Optional secondary author
|
||||
|
|
@ -53,21 +58,23 @@ class BookWithOptionalAltEditor(models.Model):
|
|||
('author', 'title', 'alt_editor'),
|
||||
)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AlternateBook(Book):
|
||||
notes = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s - %s' % (self.title, self.notes)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AuthorMeeting(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
authors = models.ManyToManyField(Author)
|
||||
created = models.DateField(editable=False)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class CustomPrimaryKey(models.Model):
|
||||
|
|
@ -77,19 +84,21 @@ class CustomPrimaryKey(models.Model):
|
|||
|
||||
# models for inheritance tests.
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
city = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Owner(models.Model):
|
||||
auto_id = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=100)
|
||||
place = models.ForeignKey(Place)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s at %s" % (self.name, self.place)
|
||||
|
||||
class Location(models.Model):
|
||||
|
|
@ -98,30 +107,34 @@ class Location(models.Model):
|
|||
lat = models.CharField(max_length=100)
|
||||
lon = models.CharField(max_length=100)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class OwnerProfile(models.Model):
|
||||
owner = models.OneToOneField(Owner, primary_key=True)
|
||||
age = models.PositiveIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is %d" % (self.owner.name, self.age)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Restaurant(Place):
|
||||
serves_pizza = models.BooleanField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Product(models.Model):
|
||||
slug = models.SlugField(unique=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.slug
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Price(models.Model):
|
||||
price = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
quantity = models.PositiveIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s for %s" % (self.quantity, self.price)
|
||||
|
||||
class Meta:
|
||||
|
|
@ -136,12 +149,14 @@ class ClassyMexicanRestaurant(MexicanRestaurant):
|
|||
|
||||
# models for testing unique_together validation when a fk is involved and
|
||||
# using inlineformset_factory.
|
||||
@python_2_unicode_compatible
|
||||
class Repository(models.Model):
|
||||
name = models.CharField(max_length=25)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Revision(models.Model):
|
||||
repository = models.ForeignKey(Repository)
|
||||
revision = models.CharField(max_length=40)
|
||||
|
|
@ -149,7 +164,7 @@ class Revision(models.Model):
|
|||
class Meta:
|
||||
unique_together = (("repository", "revision"),)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s (%s)" % (self.revision, six.text_type(self.repository))
|
||||
|
||||
# models for testing callable defaults (see bug #7975). If you define a model
|
||||
|
|
@ -167,32 +182,36 @@ class Membership(models.Model):
|
|||
class Team(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Player(models.Model):
|
||||
team = models.ForeignKey(Team, null=True)
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
# Models for testing custom ModelForm save methods in formsets and inline formsets
|
||||
@python_2_unicode_compatible
|
||||
class Poet(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Poem(models.Model):
|
||||
poet = models.ForeignKey(Poet)
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
||||
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
||||
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
||||
posted = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue