Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Commit

Permalink
速度に大差なかったので戻した
Browse files Browse the repository at this point in the history
  • Loading branch information
giginet committed Dec 26, 2015
1 parent 9010f64 commit 3f8abaa
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/lib/django-activities/activities/models.py
Expand Up @@ -6,6 +6,7 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey
from django.db.models import Max
from django.utils.translation import ugettext as _


Expand All @@ -24,13 +25,11 @@ def latests(self):
Return latest activities of each particular content_objects
"""
qs = super().get_queryset()
qs = qs.raw(
'SELECT * FROM '
'(SELECT id, MAX(id) as max_id FROM activities_activity GROUP BY content_type_id, object_id) '
'WHERE id = max_id ORDER BY id DESC'
)
qs.count = lambda: len(list(qs))
return qs
qs = qs.values('content_type_id', 'object_id')
qs = qs.annotate(pk=Max('pk'))
pks = qs.values_list('pk', flat=True)
# return activities corresponding to the latests
return self.filter(pk__in=pks)

def get_for_model(self, model):
"""
Expand Down

0 comments on commit 3f8abaa

Please sign in to comment.