2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
8. get_latest_by
|
|
|
|
|
|
|
|
Models can have a ``get_latest_by`` attribute, which should be set to the name
|
2008-08-12 14:15:38 +00:00
|
|
|
of a ``DateField`` or ``DateTimeField``. If ``get_latest_by`` exists, the
|
|
|
|
model's manager will get a ``latest()`` method, which will return the latest
|
|
|
|
object in the database according to that field. "Latest" means "having the date
|
|
|
|
farthest into the future."
|
2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2012-08-12 10:32:08 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
class Article(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
headline = models.CharField(max_length=100)
|
2006-05-02 01:31:56 +00:00
|
|
|
pub_date = models.DateField()
|
|
|
|
expire_date = models.DateField()
|
|
|
|
class Meta:
|
|
|
|
get_latest_by = 'pub_date'
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.headline
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 01:31:56 +00:00
|
|
|
class Person(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=30)
|
2006-05-02 01:31:56 +00:00
|
|
|
birthday = models.DateField()
|
|
|
|
|
|
|
|
# Note that this model doesn't have "get_latest_by" set.
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|