/tests/modeltests/get_latest/models.py

https://code.google.com/p/mango-py/ · Python · 30 lines · 15 code · 1 blank · 14 comment · 0 complexity · b0673fc4b9e172824b14ad31285d70fe MD5 · raw file

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