1

我的模型:

命中计数器:

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

Maybe the meta options order_with_respect_to and ordering help, if you want to have this ordering by default.

Your model would look like this then:

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 回答