=== modified file 'django/db/models/query.py'
|
|
|
353 | 353 | "Cannot change a query once a slice has been taken." |
354 | 354 | obj = self._clone() |
355 | 355 | obj.query.set_limits(high=1) |
356 | | obj.query.add_ordering('-%s' % latest_by) |
| 356 | if isinstance(latest_by, (list, tuple)): |
| 357 | for s in latest_by: |
| 358 | obj.query.add_ordering('-%s' % s) |
| 359 | else: |
| 360 | obj.query.add_ordering('-%s' % latest_by) |
357 | 361 | return obj.get() |
358 | 362 | |
359 | 363 | def in_bulk(self, id_list): |
=== modified file 'tests/modeltests/get_latest/models.py'
|
|
|
29 | 29 | def __unicode__(self): |
30 | 30 | return self.name |
31 | 31 | |
| 32 | class Transaction(models.Model): |
| 33 | sort_order = models.IntegerField() |
| 34 | timestamp = models.DateTimeField() |
| 35 | |
| 36 | class Meta: |
| 37 | ordering = ('sort_order', 'timestamp') |
| 38 | get_latest_by = ordering |
| 39 | |
| 40 | def __unicode__(self): |
| 41 | return u'#%u: %s' % (self.id, self.timestamp) |
| 42 | |
32 | 43 | __test__ = {'API_TESTS':""" |
33 | 44 | # Because no Articles exist yet, latest() raises ArticleDoesNotExist. |
34 | 45 | >>> Article.objects.latest() |
… |
… |
|
76 | 87 | |
77 | 88 | >>> Person.objects.latest('birthday') |
78 | 89 | <Person: Stephanie> |
| 90 | |
| 91 | >>> ts = datetime(2009, 7, 3, 17, 45, 30) |
| 92 | >>> trans1 = Transaction.objects.create(timestamp = ts, sort_order = 1) |
| 93 | >>> trans2 = Transaction.objects.create(timestamp = ts, sort_order = 2) |
| 94 | >>> Transaction.objects.all() |
| 95 | [<Transaction: #1: 2009-07-03 17:45:30>, <Transaction: #2: 2009-07-03 17:45:30>] |
| 96 | >>> Transaction.objects.latest() |
| 97 | <Transaction: #2: 2009-07-03 17:45:30> |
| 98 | >>> trans1.sort_order = 3 |
| 99 | >>> trans1.save() |
| 100 | >>> Transaction.objects.latest() |
| 101 | <Transaction: #1: 2009-07-03 17:45:30> |
79 | 102 | """} |