1

私のモデル:

HitCounter:

hits  = models.PositiveIntegerField(default=0)
object_pk  = models.TextField('object ID')
content_type    = models.ForeignKey(ContentType,
                        verbose_name="content cype",
                        related_name="content_type_set_for_%(class)s",)
content_object  = generic.GenericForeignKey('content_type', 'object_pk')

アイテム:

title    = models.CharField(_('Title'), max_length=400)
desc     = models.TextField(_('Description'), blank=True

)。

HitCounterのヒットでItemのアイテムを並べ替えたいですか?私は何をすべきか?

4

2 に答える 2

2

私はこれがうまくいくと思います:

items = Item.objects.annotate(hits=Sum('hitcounter__hits')).order_by('-hits')

Djangoアグリゲーションのドキュメントはこちら

于 2010-02-28T19:05:58.797 に答える
0

デフォルトでこの順序を使用したい場合は、メタオプションorder_with_respect_toとヘルプを使用してください。ordering

その場合、モデルは次のようになります。

class HitCounter:
    # properties here

    class Meta:
        order_with_respect_to: 'content_object'  # not sure if this really works
        ordering: ['-hits']
于 2010-02-28T21:03:43.400 に答える