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

Commit

Permalink
Merge pull request #1019 from kawazrepos/modify-sql2
Browse files Browse the repository at this point in the history
速度に大差なかったので戻した
  • Loading branch information
giginet committed Dec 26, 2015
2 parents d491a76 + 3f8abaa commit f1806fe
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/lib/django-activities/activities/models.py
Original file line number Diff line number Diff line change
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 *, MAX(id) AS max_id FROM activities_activity GROUP BY '
'content_type_id, object_id HAVING 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 f1806fe

Please sign in to comment.