class Person(models.Model):
name = models.CharField(max_length=128)
def __unicode__(self):
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __unicode__(self):
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
reputation = models.IntegerField(default=0)
我想按照模型会员资格对人的order_by声誉进行排序。就像这样:
person_ids = Membership.objects.all().order_by('-reputation').values_list('person_id', flat=True)
persons = Person.objects.filter(id__in=person_ids) # Such as [3, 1, 2]
但结果QuerySet的顺序不正确, SQL IN 是无订单搜索。
我想结果人是一个查询集,所以我不想列出人并订购它们 排序函数。
我该怎么办?
答案 0 :(得分:1)
Person.objects.order_by( ' - membership__reputation')