In Django (1.0.2), I have 2 models: Lesson and StatLesson.
class Lesson(models.Model):
contents = models.TextField()
def get_visits(self):
return self.statlesson_set.all().count()
class StatLesson(models.Model):
lesson = models.ForeignKey(Lesson)
datetime = models.DateTimeField(default=datetime.datetime.now())
Each StatLesson registers 1 visit of a certain Lesson. I can use lesson.get_visits() to get the number of visits for that lesson.
How do I get a queryset of lessons, that's sorted by the number of visits? I'm looking for something like this: Lesson.objects.all().order_by('statlesson__count') (but this obviously doesn't work)