parts/django/tests/modeltests/get_latest/models.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 """
       
     2 8. get_latest_by
       
     3 
       
     4 Models can have a ``get_latest_by`` attribute, which should be set to the name
       
     5 of a ``DateField`` or ``DateTimeField``. If ``get_latest_by`` exists, the
       
     6 model's manager will get a ``latest()`` method, which will return the latest
       
     7 object in the database according to that field. "Latest" means "having the date
       
     8 farthest into the future."
       
     9 """
       
    10 
       
    11 from django.db import models
       
    12 
       
    13 class Article(models.Model):
       
    14     headline = models.CharField(max_length=100)
       
    15     pub_date = models.DateField()
       
    16     expire_date = models.DateField()
       
    17     class Meta:
       
    18         get_latest_by = 'pub_date'
       
    19 
       
    20     def __unicode__(self):
       
    21         return self.headline
       
    22 
       
    23 class Person(models.Model):
       
    24     name = models.CharField(max_length=30)
       
    25     birthday = models.DateField()
       
    26 
       
    27     # Note that this model doesn't have "get_latest_by" set.
       
    28 
       
    29     def __unicode__(self):
       
    30         return self.name