| 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 |
|---|
| 31 |
|
|---|
| 32 |
__test__ = {'API_TESTS':""" |
|---|
| 33 |
# Because no Articles exist yet, latest() raises ArticleDoesNotExist. |
|---|
| 34 |
>>> Article.objects.latest() |
|---|
| 35 |
Traceback (most recent call last): |
|---|
| 36 |
... |
|---|
| 37 |
DoesNotExist: Article matching query does not exist. |
|---|
| 38 |
|
|---|
| 39 |
# Create a couple of Articles. |
|---|
| 40 |
>>> from datetime import datetime |
|---|
| 41 |
>>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1)) |
|---|
| 42 |
>>> a1.save() |
|---|
| 43 |
>>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28)) |
|---|
| 44 |
>>> a2.save() |
|---|
| 45 |
>>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27)) |
|---|
| 46 |
>>> a3.save() |
|---|
| 47 |
>>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30)) |
|---|
| 48 |
>>> a4.save() |
|---|
| 49 |
|
|---|
| 50 |
# Get the latest Article. |
|---|
| 51 |
>>> Article.objects.latest() |
|---|
| 52 |
<Article: Article 4> |
|---|
| 53 |
|
|---|
| 54 |
# Get the latest Article that matches certain filters. |
|---|
| 55 |
>>> Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest() |
|---|
| 56 |
<Article: Article 1> |
|---|
| 57 |
|
|---|
| 58 |
# Pass a custom field name to latest() to change the field that's used to |
|---|
| 59 |
# determine the latest object. |
|---|
| 60 |
>>> Article.objects.latest('expire_date') |
|---|
| 61 |
<Article: Article 1> |
|---|
| 62 |
|
|---|
| 63 |
>>> Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date') |
|---|
| 64 |
<Article: Article 3> |
|---|
| 65 |
|
|---|
| 66 |
# You can still use latest() with a model that doesn't have "get_latest_by" |
|---|
| 67 |
# set -- just pass in the field name manually. |
|---|
| 68 |
>>> p1 = Person(name='Ralph', birthday=datetime(1950, 1, 1)) |
|---|
| 69 |
>>> p1.save() |
|---|
| 70 |
>>> p2 = Person(name='Stephanie', birthday=datetime(1960, 2, 3)) |
|---|
| 71 |
>>> p2.save() |
|---|
| 72 |
>>> Person.objects.latest() |
|---|
| 73 |
Traceback (most recent call last): |
|---|
| 74 |
... |
|---|
| 75 |
AssertionError: latest() requires either a field_name parameter or 'get_latest_by' in the model |
|---|
| 76 |
|
|---|
| 77 |
>>> Person.objects.latest('birthday') |
|---|
| 78 |
<Person: Stephanie> |
|---|
| 79 |
"""} |
|---|