PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/get_latest/models.py

https://code.google.com/p/mango-py/
Python | 30 lines | 15 code | 1 blank | 14 comment | 1 complexity | b0673fc4b9e172824b14ad31285d70fe MD5 | raw file
 1"""
 28. get_latest_by
 3
 4Models can have a ``get_latest_by`` attribute, which should be set to the name
 5of a ``DateField`` or ``DateTimeField``. If ``get_latest_by`` exists, the
 6model's manager will get a ``latest()`` method, which will return the latest
 7object in the database according to that field. "Latest" means "having the date
 8farthest into the future."
 9"""
10
11from django.db import models
12
13class 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
23class 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