Ticket #11557: django-get-latest-by-tuple.diff

File django-get-latest-by-tuple.diff, 1.9 KB (added by Forest Bond, 15 years ago)

Patch implementing requested feature.

  • django/db/models/query.py

    === modified file 'django/db/models/query.py'
     
    353353                "Cannot change a query once a slice has been taken."
    354354        obj = self._clone()
    355355        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)
    357361        return obj.get()
    358362
    359363    def in_bulk(self, id_list):
  • tests/modeltests/get_latest/models.py

    === modified file 'tests/modeltests/get_latest/models.py'
     
    2929    def __unicode__(self):
    3030        return self.name
    3131
     32class 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
    3243__test__ = {'API_TESTS':"""
    3344# Because no Articles exist yet, latest() raises ArticleDoesNotExist.
    3445>>> Article.objects.latest()
     
    7687
    7788>>> Person.objects.latest('birthday')
    7889<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>
    79102"""}
Back to Top