Kaydet (Commit) e817ae74 authored tarafından Matt Wiens's avatar Matt Wiens Kaydeden (comit) Tim Graham

Followed style guide for model attribute ordering.

üst dd8ed641
...@@ -59,22 +59,22 @@ class AbstractBaseUser(models.Model): ...@@ -59,22 +59,22 @@ class AbstractBaseUser(models.Model):
class Meta: class Meta:
abstract = True abstract = True
def get_username(self):
"Return the identifying username for this User"
return getattr(self, self.USERNAME_FIELD)
def __str__(self): def __str__(self):
return self.get_username() return self.get_username()
def clean(self):
setattr(self, self.USERNAME_FIELD, self.normalize_username(self.get_username()))
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super().save(*args, **kwargs) super().save(*args, **kwargs)
if self._password is not None: if self._password is not None:
password_validation.password_changed(self._password, self) password_validation.password_changed(self._password, self)
self._password = None self._password = None
def get_username(self):
"""Return the username for this User."""
return getattr(self, self.USERNAME_FIELD)
def clean(self):
setattr(self, self.USERNAME_FIELD, self.normalize_username(self.get_username()))
def natural_key(self): def natural_key(self):
return (self.get_username(),) return (self.get_username(),)
......
...@@ -60,6 +60,7 @@ class Permission(models.Model): ...@@ -60,6 +60,7 @@ class Permission(models.Model):
verbose_name=_('content type'), verbose_name=_('content type'),
) )
codename = models.CharField(_('codename'), max_length=100) codename = models.CharField(_('codename'), max_length=100)
objects = PermissionManager() objects = PermissionManager()
class Meta: class Meta:
......
...@@ -23,6 +23,9 @@ class OracleGeometryColumns(models.Model): ...@@ -23,6 +23,9 @@ class OracleGeometryColumns(models.Model):
db_table = 'USER_SDO_GEOM_METADATA' db_table = 'USER_SDO_GEOM_METADATA'
managed = False managed = False
def __str__(self):
return '%s - %s (SRID: %s)' % (self.table_name, self.column_name, self.srid)
@classmethod @classmethod
def table_name_col(cls): def table_name_col(cls):
""" """
...@@ -39,9 +42,6 @@ class OracleGeometryColumns(models.Model): ...@@ -39,9 +42,6 @@ class OracleGeometryColumns(models.Model):
""" """
return 'column_name' return 'column_name'
def __str__(self):
return '%s - %s (SRID: %s)' % (self.table_name, self.column_name, self.srid)
class OracleSpatialRefSys(models.Model, SpatialRefSysMixin): class OracleSpatialRefSys(models.Model, SpatialRefSysMixin):
"Maps to the Oracle MDSYS.CS_SRS table." "Maps to the Oracle MDSYS.CS_SRS table."
......
...@@ -23,6 +23,15 @@ class PostGISGeometryColumns(models.Model): ...@@ -23,6 +23,15 @@ class PostGISGeometryColumns(models.Model):
db_table = 'geometry_columns' db_table = 'geometry_columns'
managed = False managed = False
def __str__(self):
return '%s.%s - %dD %s field (SRID: %d)' % (
self.f_table_name,
self.f_geometry_column,
self.coord_dimension,
self.type,
self.srid,
)
@classmethod @classmethod
def table_name_col(cls): def table_name_col(cls):
""" """
...@@ -39,11 +48,6 @@ class PostGISGeometryColumns(models.Model): ...@@ -39,11 +48,6 @@ class PostGISGeometryColumns(models.Model):
""" """
return 'f_geometry_column' return 'f_geometry_column'
def __str__(self):
return "%s.%s - %dD %s field (SRID: %d)" % \
(self.f_table_name, self.f_geometry_column,
self.coord_dimension, self.type, self.srid)
class PostGISSpatialRefSys(models.Model, SpatialRefSysMixin): class PostGISSpatialRefSys(models.Model, SpatialRefSysMixin):
""" """
......
...@@ -21,6 +21,15 @@ class SpatialiteGeometryColumns(models.Model): ...@@ -21,6 +21,15 @@ class SpatialiteGeometryColumns(models.Model):
db_table = 'geometry_columns' db_table = 'geometry_columns'
managed = False managed = False
def __str__(self):
return '%s.%s - %dD %s field (SRID: %d)' % (
self.f_table_name,
self.f_geometry_column,
self.coord_dimension,
self.type,
self.srid,
)
@classmethod @classmethod
def table_name_col(cls): def table_name_col(cls):
""" """
...@@ -37,11 +46,6 @@ class SpatialiteGeometryColumns(models.Model): ...@@ -37,11 +46,6 @@ class SpatialiteGeometryColumns(models.Model):
""" """
return 'f_geometry_column' return 'f_geometry_column'
def __str__(self):
return "%s.%s - %dD %s field (SRID: %d)" % \
(self.f_table_name, self.f_geometry_column,
self.coord_dimension, self.type, self.srid)
class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin): class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
""" """
...@@ -54,11 +58,11 @@ class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin): ...@@ -54,11 +58,11 @@ class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
proj4text = models.CharField(max_length=2048) proj4text = models.CharField(max_length=2048)
srtext = models.CharField(max_length=2048) srtext = models.CharField(max_length=2048)
@property
def wkt(self):
return self.srtext
class Meta: class Meta:
app_label = 'gis' app_label = 'gis'
db_table = 'spatial_ref_sys' db_table = 'spatial_ref_sys'
managed = False managed = False
@property
def wkt(self):
return self.srtext
...@@ -84,6 +84,7 @@ class Site(models.Model): ...@@ -84,6 +84,7 @@ class Site(models.Model):
unique=True, unique=True,
) )
name = models.CharField(_('display name'), max_length=50) name = models.CharField(_('display name'), max_length=50)
objects = SiteManager() objects = SiteManager()
class Meta: class Meta:
......
...@@ -17,22 +17,22 @@ objects, and a ``Publication`` has multiple ``Article`` objects: ...@@ -17,22 +17,22 @@ objects, and a ``Publication`` has multiple ``Article`` objects:
class Publication(models.Model): class Publication(models.Model):
title = models.CharField(max_length=30) title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
def __str__(self):
return self.title
class Article(models.Model): class Article(models.Model):
headline = models.CharField(max_length=100) headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication) publications = models.ManyToManyField(Publication)
def __str__(self):
return self.headline
class Meta: class Meta:
ordering = ('headline',) ordering = ('headline',)
def __str__(self):
return self.headline
What follows are examples of operations that can be performed using the Python What follows are examples of operations that can be performed using the Python
API facilities. Note that if you are using :ref:`an intermediate model API facilities. Note that if you are using :ref:`an intermediate model
<intermediary-manytomany>` for a many-to-many relationship, some of the related <intermediary-manytomany>` for a many-to-many relationship, some of the related
......
...@@ -404,13 +404,12 @@ name:: ...@@ -404,13 +404,12 @@ name::
return self.get(first_name=first_name, last_name=last_name) return self.get(first_name=first_name, last_name=last_name)
class Person(models.Model): class Person(models.Model):
objects = PersonManager()
first_name = models.CharField(max_length=100) first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)
birthdate = models.DateField() birthdate = models.DateField()
objects = PersonManager()
class Meta: class Meta:
unique_together = (('first_name', 'last_name'),) unique_together = (('first_name', 'last_name'),)
...@@ -453,19 +452,18 @@ So how do you get Django to emit a natural key when serializing an object? ...@@ -453,19 +452,18 @@ So how do you get Django to emit a natural key when serializing an object?
Firstly, you need to add another method -- this time to the model itself:: Firstly, you need to add another method -- this time to the model itself::
class Person(models.Model): class Person(models.Model):
objects = PersonManager()
first_name = models.CharField(max_length=100) first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)
birthdate = models.DateField() birthdate = models.DateField()
def natural_key(self): objects = PersonManager()
return (self.first_name, self.last_name)
class Meta: class Meta:
unique_together = (('first_name', 'last_name'),) unique_together = (('first_name', 'last_name'),)
def natural_key(self):
return (self.first_name, self.last_name)
That method should always return a natural key tuple -- in this That method should always return a natural key tuple -- in this
example, ``(first name, last name)``. Then, when you call example, ``(first name, last name)``. Then, when you call
``serializers.serialize()``, you provide ``use_natural_foreign_keys=True`` ``serializers.serialize()``, you provide ``use_natural_foreign_keys=True``
......
...@@ -80,13 +80,13 @@ class Chapter(models.Model): ...@@ -80,13 +80,13 @@ class Chapter(models.Model):
content = models.TextField() content = models.TextField()
book = models.ForeignKey(Book, models.CASCADE) book = models.ForeignKey(Book, models.CASCADE)
def __str__(self):
return self.title
class Meta: class Meta:
# Use a utf-8 bytestring to ensure it works (see #11710) # Use a utf-8 bytestring to ensure it works (see #11710)
verbose_name = '¿Chapter?' verbose_name = '¿Chapter?'
def __str__(self):
return self.title
class ChapterXtra1(models.Model): class ChapterXtra1(models.Model):
chap = models.OneToOneField(Chapter, models.CASCADE, verbose_name='¿Chap?') chap = models.OneToOneField(Chapter, models.CASCADE, verbose_name='¿Chap?')
......
...@@ -135,12 +135,12 @@ class Advisor(models.Model): ...@@ -135,12 +135,12 @@ class Advisor(models.Model):
class Student(models.Model): class Student(models.Model):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __str__(self):
return self.name
class School(models.Model): class School(models.Model):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
......
...@@ -23,13 +23,13 @@ class Author(models.Model): ...@@ -23,13 +23,13 @@ class Author(models.Model):
first_name = models.CharField(max_length=30, db_column='firstname') first_name = models.CharField(max_length=30, db_column='firstname')
last_name = models.CharField(max_length=30, db_column='last') last_name = models.CharField(max_length=30, db_column='last')
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
class Meta: class Meta:
db_table = 'my_author_table' db_table = 'my_author_table'
ordering = ('last_name', 'first_name') ordering = ('last_name', 'first_name')
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
class Article(models.Model): class Article(models.Model):
Article_ID = models.AutoField(primary_key=True, db_column='Article ID') Article_ID = models.AutoField(primary_key=True, db_column='Article ID')
...@@ -43,8 +43,8 @@ class Article(models.Model): ...@@ -43,8 +43,8 @@ class Article(models.Model):
null=True, null=True,
) )
def __str__(self):
return self.headline
class Meta: class Meta:
ordering = ('headline',) ordering = ('headline',)
def __str__(self):
return self.headline
...@@ -20,23 +20,23 @@ class Category(models.Model): ...@@ -20,23 +20,23 @@ class Category(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
description = models.TextField() description = models.TextField()
def __str__(self):
return self.title
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
def __str__(self):
return self.title
class Article(models.Model): class Article(models.Model):
headline = models.CharField(max_length=100, default='Default headline') headline = models.CharField(max_length=100, default='Default headline')
pub_date = models.DateTimeField() pub_date = models.DateTimeField()
def __str__(self):
return self.headline
class Meta: class Meta:
ordering = ('-pub_date', 'headline') ordering = ('-pub_date', 'headline')
def __str__(self):
return self.headline
class Blog(models.Model): class Blog(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
...@@ -68,12 +68,12 @@ class Person(models.Model): ...@@ -68,12 +68,12 @@ class Person(models.Model):
objects = PersonManager() objects = PersonManager()
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __str__(self):
return self.name
def natural_key(self): def natural_key(self):
return (self.name,) return (self.name,)
...@@ -106,13 +106,13 @@ class Book(models.Model): ...@@ -106,13 +106,13 @@ class Book(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
authors = models.ManyToManyField(Person) authors = models.ManyToManyField(Person)
class Meta:
ordering = ('name',)
def __str__(self): def __str__(self):
authors = ' and '.join(a.name for a in self.authors.all()) authors = ' and '.join(a.name for a in self.authors.all())
return '%s by %s' % (self.name, authors) if authors else self.name return '%s by %s' % (self.name, authors) if authors else self.name
class Meta:
ordering = ('name',)
class PrimaryKeyUUIDModel(models.Model): class PrimaryKeyUUIDModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4) id = models.UUIDField(primary_key=True, default=uuid.uuid4)
......
...@@ -5,9 +5,9 @@ class Article(models.Model): ...@@ -5,9 +5,9 @@ class Article(models.Model):
headline = models.CharField(max_length=100, default='Default headline') headline = models.CharField(max_length=100, default='Default headline')
pub_date = models.DateTimeField() pub_date = models.DateTimeField()
def __str__(self):
return self.headline
class Meta: class Meta:
app_label = 'fixtures_model_package' app_label = 'fixtures_model_package'
ordering = ('-pub_date', 'headline') ordering = ('-pub_date', 'headline')
def __str__(self):
return self.headline
...@@ -98,10 +98,11 @@ class TestManager(models.Manager): ...@@ -98,10 +98,11 @@ class TestManager(models.Manager):
class Store(models.Model): class Store(models.Model):
objects = TestManager()
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
main = models.ForeignKey('self', models.SET_NULL, null=True) main = models.ForeignKey('self', models.SET_NULL, null=True)
objects = TestManager()
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
...@@ -113,9 +114,10 @@ class Store(models.Model): ...@@ -113,9 +114,10 @@ class Store(models.Model):
class Person(models.Model): class Person(models.Model):
objects = TestManager()
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
objects = TestManager()
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
...@@ -245,6 +247,7 @@ class BaseNKModel(models.Model): ...@@ -245,6 +247,7 @@ class BaseNKModel(models.Model):
Base model with a natural_key and a manager with `get_by_natural_key` Base model with a natural_key and a manager with `get_by_natural_key`
""" """
data = models.CharField(max_length=20, unique=True) data = models.CharField(max_length=20, unique=True)
objects = NKManager() objects = NKManager()
class Meta: class Meta:
......
...@@ -41,9 +41,6 @@ class Article(models.Model): ...@@ -41,9 +41,6 @@ class Article(models.Model):
response_to = models.ForeignKey('self', models.SET_NULL, null=True) response_to = models.ForeignKey('self', models.SET_NULL, null=True)
unmanaged_reporters = models.ManyToManyField(Reporter, through='ArticleReporter', related_name='+') unmanaged_reporters = models.ManyToManyField(Reporter, through='ArticleReporter', related_name='+')
def __str__(self):
return self.headline
class Meta: class Meta:
ordering = ('headline',) ordering = ('headline',)
index_together = [ index_together = [
...@@ -51,6 +48,9 @@ class Article(models.Model): ...@@ -51,6 +48,9 @@ class Article(models.Model):
['headline', 'response_to', 'pub_date', 'reporter'], ['headline', 'response_to', 'pub_date', 'reporter'],
] ]
def __str__(self):
return self.headline
class ArticleReporter(models.Model): class ArticleReporter(models.Model):
article = models.ForeignKey(Article, models.CASCADE) article = models.ForeignKey(Article, models.CASCADE)
......
...@@ -15,12 +15,12 @@ class Issue(models.Model): ...@@ -15,12 +15,12 @@ class Issue(models.Model):
cc = models.ManyToManyField(User, blank=True, related_name='test_issue_cc') cc = models.ManyToManyField(User, blank=True, related_name='test_issue_cc')
client = models.ForeignKey(User, models.CASCADE, related_name='test_issue_client') client = models.ForeignKey(User, models.CASCADE, related_name='test_issue_client')
def __str__(self):
return str(self.num)
class Meta: class Meta:
ordering = ('num',) ordering = ('num',)
def __str__(self):
return str(self.num)
class StringReferenceModel(models.Model): class StringReferenceModel(models.Model):
others = models.ManyToManyField('StringReferenceModel') others = models.ManyToManyField('StringReferenceModel')
...@@ -73,12 +73,12 @@ class User(models.Model): ...@@ -73,12 +73,12 @@ class User(models.Model):
class BadModelWithSplit(models.Model): class BadModelWithSplit(models.Model):
name = models.CharField(max_length=1) name = models.CharField(max_length=1)
def split(self):
raise RuntimeError('split should not be called')
class Meta: class Meta:
abstract = True abstract = True
def split(self):
raise RuntimeError('split should not be called')
class RegressionModelSplit(BadModelWithSplit): class RegressionModelSplit(BadModelWithSplit):
""" """
......
...@@ -55,13 +55,13 @@ class CustomMembership(models.Model): ...@@ -55,13 +55,13 @@ class CustomMembership(models.Model):
weird_fk = models.ForeignKey(Membership, models.SET_NULL, null=True) weird_fk = models.ForeignKey(Membership, models.SET_NULL, null=True)
date_joined = models.DateTimeField(default=datetime.now) date_joined = models.DateTimeField(default=datetime.now)
def __str__(self):
return "%s is a member of %s" % (self.person.name, self.group.name)
class Meta: class Meta:
db_table = "test_table" db_table = "test_table"
ordering = ["date_joined"] ordering = ["date_joined"]
def __str__(self):
return "%s is a member of %s" % (self.person.name, self.group.name)
class TestNoDefaultsOrNulls(models.Model): class TestNoDefaultsOrNulls(models.Model):
person = models.ForeignKey(Person, models.CASCADE) person = models.ForeignKey(Person, models.CASCADE)
......
...@@ -52,12 +52,12 @@ class Car(models.Model): ...@@ -52,12 +52,12 @@ class Car(models.Model):
class Driver(models.Model): class Driver(models.Model):
name = models.CharField(max_length=20, unique=True, null=True) name = models.CharField(max_length=20, unique=True, null=True)
def __str__(self):
return "%s" % self.name
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __str__(self):
return "%s" % self.name
class CarDriver(models.Model): class CarDriver(models.Model):
car = models.ForeignKey('Car', models.CASCADE, to_field='make') car = models.ForeignKey('Car', models.CASCADE, to_field='make')
......
...@@ -12,12 +12,12 @@ from django.db import models ...@@ -12,12 +12,12 @@ from django.db import models
class Publication(models.Model): class Publication(models.Model):
title = models.CharField(max_length=30) title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
def __str__(self):
return self.title
class Tag(models.Model): class Tag(models.Model):
id = models.BigAutoField(primary_key=True) id = models.BigAutoField(primary_key=True)
...@@ -34,12 +34,12 @@ class Article(models.Model): ...@@ -34,12 +34,12 @@ class Article(models.Model):
publications = models.ManyToManyField(Publication, name='publications') publications = models.ManyToManyField(Publication, name='publications')
tags = models.ManyToManyField(Tag, related_name='tags') tags = models.ManyToManyField(Tag, related_name='tags')
def __str__(self):
return self.headline
class Meta: class Meta:
ordering = ('headline',) ordering = ('headline',)
def __str__(self):
return self.headline
# Models to test correct related_name inheritance # Models to test correct related_name inheritance
class AbstractArticle(models.Model): class AbstractArticle(models.Model):
......
...@@ -20,12 +20,12 @@ class Article(models.Model): ...@@ -20,12 +20,12 @@ class Article(models.Model):
pub_date = models.DateField() pub_date = models.DateField()
reporter = models.ForeignKey(Reporter, models.CASCADE) reporter = models.ForeignKey(Reporter, models.CASCADE)
def __str__(self):
return self.headline
class Meta: class Meta:
ordering = ('headline',) ordering = ('headline',)
def __str__(self):
return self.headline
class City(models.Model): class City(models.Model):
id = models.BigAutoField(primary_key=True) id = models.BigAutoField(primary_key=True)
......
...@@ -215,12 +215,12 @@ class Price(models.Model): ...@@ -215,12 +215,12 @@ class Price(models.Model):
price = models.DecimalField(max_digits=10, decimal_places=2) price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField() quantity = models.PositiveIntegerField()
def __str__(self):
return "%s for %s" % (self.quantity, self.price)
class Meta: class Meta:
unique_together = (('price', 'quantity'),) unique_together = (('price', 'quantity'),)
def __str__(self):
return "%s for %s" % (self.quantity, self.price)
class Triple(models.Model): class Triple(models.Model):
left = models.IntegerField() left = models.IntegerField()
......
...@@ -135,12 +135,12 @@ class Price(models.Model): ...@@ -135,12 +135,12 @@ class Price(models.Model):
price = models.DecimalField(max_digits=10, decimal_places=2) price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField() quantity = models.PositiveIntegerField()
def __str__(self):
return "%s for %s" % (self.quantity, self.price)
class Meta: class Meta:
unique_together = (('price', 'quantity'),) unique_together = (('price', 'quantity'),)
def __str__(self):
return "%s for %s" % (self.quantity, self.price)
class MexicanRestaurant(Restaurant): class MexicanRestaurant(Restaurant):
serves_tacos = models.BooleanField(default=False) serves_tacos = models.BooleanField(default=False)
......
...@@ -12,12 +12,12 @@ class Review(models.Model): ...@@ -12,12 +12,12 @@ class Review(models.Model):
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()
content_object = GenericForeignKey() content_object = GenericForeignKey()
def __str__(self):
return self.source
class Meta: class Meta:
ordering = ('source',) ordering = ('source',)
def __str__(self):
return self.source
class PersonManager(models.Manager): class PersonManager(models.Manager):
def get_by_natural_key(self, name): def get_by_natural_key(self, name):
...@@ -25,15 +25,16 @@ class PersonManager(models.Manager): ...@@ -25,15 +25,16 @@ class PersonManager(models.Manager):
class Person(models.Model): class Person(models.Model):
objects = PersonManager()
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
def __str__(self): objects = PersonManager()
return self.name
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __str__(self):
return self.name
# This book manager doesn't do anything interesting; it just # This book manager doesn't do anything interesting; it just
# exists to strip out the 'extra_arg' argument to certain # exists to strip out the 'extra_arg' argument to certain
...@@ -48,7 +49,6 @@ class BookManager(models.Manager): ...@@ -48,7 +49,6 @@ class BookManager(models.Manager):
class Book(models.Model): class Book(models.Model):
objects = BookManager()
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
published = models.DateField() published = models.DateField()
authors = models.ManyToManyField(Person) authors = models.ManyToManyField(Person)
...@@ -56,23 +56,25 @@ class Book(models.Model): ...@@ -56,23 +56,25 @@ class Book(models.Model):
reviews = GenericRelation(Review) reviews = GenericRelation(Review)
pages = models.IntegerField(default=100) pages = models.IntegerField(default=100)
def __str__(self): objects = BookManager()
return self.title
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
def __str__(self):
return self.title
class Pet(models.Model): class Pet(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
owner = models.ForeignKey(Person, models.CASCADE) owner = models.ForeignKey(Person, models.CASCADE)
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __str__(self):
return self.name
class UserProfile(models.Model): class UserProfile(models.Model):
user = models.OneToOneField(User, models.SET_NULL, null=True) user = models.OneToOneField(User, models.SET_NULL, null=True)
......
...@@ -17,12 +17,12 @@ class Article(models.Model): ...@@ -17,12 +17,12 @@ class Article(models.Model):
title = models.CharField(max_length=150) title = models.CharField(max_length=150)
author = models.ForeignKey(Author, models.SET_NULL, null=True) author = models.ForeignKey(Author, models.SET_NULL, null=True)
def __str__(self):
return 'Article titled: %s' % self.title
class Meta: class Meta:
ordering = ['author__name'] ordering = ['author__name']
def __str__(self):
return 'Article titled: %s' % self.title
# These following 4 models represent a far more complex ordering case. # These following 4 models represent a far more complex ordering case.
class SystemInfo(models.Model): class SystemInfo(models.Model):
......
...@@ -15,12 +15,12 @@ class Author(models.Model): ...@@ -15,12 +15,12 @@ class Author(models.Model):
favorite_authors = models.ManyToManyField( favorite_authors = models.ManyToManyField(
'self', through='FavoriteAuthors', symmetrical=False, related_name='favors_me') 'self', through='FavoriteAuthors', symmetrical=False, related_name='favors_me')
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return self.name
class AuthorWithAge(Author): class AuthorWithAge(Author):
author = models.OneToOneField(Author, models.CASCADE, parent_link=True) author = models.OneToOneField(Author, models.CASCADE, parent_link=True)
...@@ -50,12 +50,12 @@ class Book(models.Model): ...@@ -50,12 +50,12 @@ class Book(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
authors = models.ManyToManyField(Author, related_name='books') authors = models.ManyToManyField(Author, related_name='books')
def __str__(self):
return self.title
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return self.title
class BookWithYear(Book): class BookWithYear(Book):
book = models.OneToOneField(Book, models.CASCADE, parent_link=True) book = models.OneToOneField(Book, models.CASCADE, parent_link=True)
...@@ -78,12 +78,12 @@ class Reader(models.Model): ...@@ -78,12 +78,12 @@ class Reader(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
books_read = models.ManyToManyField(Book, related_name='read_by') books_read = models.ManyToManyField(Book, related_name='read_by')
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return self.name
class BookReview(models.Model): class BookReview(models.Model):
# Intentionally does not have a related name. # Intentionally does not have a related name.
...@@ -122,12 +122,12 @@ class Teacher(models.Model): ...@@ -122,12 +122,12 @@ class Teacher(models.Model):
objects = TeacherManager() objects = TeacherManager()
objects_custom = TeacherQuerySet.as_manager() objects_custom = TeacherQuerySet.as_manager()
def __str__(self):
return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))
class Department(models.Model): class Department(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
...@@ -165,12 +165,12 @@ class TaggedItem(models.Model): ...@@ -165,12 +165,12 @@ class TaggedItem(models.Model):
favorite_fkey = models.CharField(max_length=64, null=True) favorite_fkey = models.CharField(max_length=64, null=True)
favorite = GenericForeignKey('favorite_ct', 'favorite_fkey') favorite = GenericForeignKey('favorite_ct', 'favorite_fkey')
def __str__(self):
return self.tag
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return self.tag
class Bookmark(models.Model): class Bookmark(models.Model):
url = models.URLField() url = models.URLField()
...@@ -243,12 +243,12 @@ class Employee(models.Model): ...@@ -243,12 +243,12 @@ class Employee(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
boss = models.ForeignKey('self', models.SET_NULL, null=True, related_name='serfs') boss = models.ForeignKey('self', models.SET_NULL, null=True, related_name='serfs')
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return self.name
# Ticket #19607 # Ticket #19607
...@@ -275,12 +275,12 @@ class Author2(models.Model): ...@@ -275,12 +275,12 @@ class Author2(models.Model):
first_book = models.ForeignKey('Book', models.CASCADE, related_name='first_time_authors+') first_book = models.ForeignKey('Book', models.CASCADE, related_name='first_time_authors+')
favorite_books = models.ManyToManyField('Book', related_name='+') favorite_books = models.ManyToManyField('Book', related_name='+')
def __str__(self):
return self.name
class Meta: class Meta:
ordering = ['id'] ordering = ['id']
def __str__(self):
return self.name
# Models for many-to-many with UUID pk test: # Models for many-to-many with UUID pk test:
......
...@@ -10,11 +10,11 @@ class NaturalKeyAnchorManager(models.Manager): ...@@ -10,11 +10,11 @@ class NaturalKeyAnchorManager(models.Manager):
class NaturalKeyAnchor(models.Model): class NaturalKeyAnchor(models.Model):
objects = NaturalKeyAnchorManager()
data = models.CharField(max_length=100, unique=True) data = models.CharField(max_length=100, unique=True)
title = models.CharField(max_length=100, null=True) title = models.CharField(max_length=100, null=True)
objects = NaturalKeyAnchorManager()
def natural_key(self): def natural_key(self):
return (self.data,) return (self.data,)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment