我觉得你把事情弄得太难了,你自己可能也做得太多了。
你可以数一数
不同的
ip_address
是的,和
distinct=True
参数的
Count
object [Django-doc]
,例如:
from django.db.models import Count
ObjectViewed.objects.values('object_id', 'content_type').annotate(
c=Count('ip_address', distinct=True)
).order_by('-c')
我们可以用它来有效地
NewsPost
就像:
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count
views = dict(ObjectViewed.objects.filter(
content_type=get_object_for_this_type(NewsPost)
).values_list('object_id').annotate(
c=Count('ip_address', distinct=True)
).order_by('object_id'))
然后我们可以在django/python级别对元素进行排序,如下所示:
my_newsposts = sorted(NewsPost.objects.all(), key=views.get, reverse=True)
因此,我们在这里首先筛选只检索
不同的
对…的看法
新闻稿
对象,我们把它们放在一个名为
views
,然后我们对
新闻稿
视图顺序相反的对象。