1
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模型成員身份信譽。就像這樣:Django QuerySet過濾器與給定的排序列表
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]
但結果查詢集是不正確的順序,SQL IN是一個沒有秩序的搜索。
我想結果人員是一個查詢集,所以我不想列出人員,並排序他們 排序函數。
我該怎麼辦?
謝謝,它運作良好。 – chenchiyuan