Django: Sort model based on difference between two of its fields

Take a simple class like this.

class MyModel(models.Model):
    last_updated = models.DateTimeField(auto_now_add=True)
    last_viewed  = models.DateTimeField(auto_now_add=True)

All I want to do is sort the output of a query on this model by the time between those two fields so that things that haven't been viewed since they were updated show higher up than things that have.

I feel like I should be able to annotate the table and sort on that annotation, but I'm no genius when it comes to annotations (or raw SQL, for that matter). Could somebody kick me in the right direction?

7
задан skaffman 28 May 2011 в 08:49
поделиться