1

I have the following self referencing model that gives who this profile supports.

    supports = models.ManyToManyField('self', blank=True, symmetrical=False,
                                  related_name='supporters')

And I want the count not supports, supporters that support this user.

Any ideas ?

def get_queryset(self):
    qs = super(ProfileListView, self).get_queryset()
    if self.request.GET.get('s'):
        sorting = self.request.GET.get('s')
        if sorting == 'pop':

            qs = ......
    return qs
4

2 回答 2

0

像下面注释支持者的数量,解决了我的问题

 qs = qs.annotate(number_of_supporters=Count('supporters'))
 qs = qs.order_by('-number_of_supporters')
于 2013-06-30T19:37:09.020 回答
0

我假设我有一个名为 myUser 的模型实例:

supporters = myUser.supports_set.all()
于 2013-06-29T13:10:38.803 回答