1
activities = Activity.objects.filter(actor__in=users).distinct('content_type', 'object_id', 'action')
If your database does not support DISTINCT ON
queries, you can do the query then manually run through the objects finding distinct values:
unique_activities = []
activities = Activity.objects.filter(actor__in=users)
for activity in activities:
unseen = True
for unique_activity in unique_activities:
if (activity.action == unique_activity.action and activity.content_type == unique_activity.content_type and activity.object_id == unique_activity.object_id):
unseen = False
break
if unseen:
unique_activities.append(activity)
print unique_activities
1
This will work only with PostgreSQL:
Activity.objects.order_by('action', 'content_type_id', 'object_id', '-pub_date').distinct('action', 'content_type_id', 'object_id').filter(actor__in=actors)
- [Answered ]-Django frontend and backend seperation for security
- [Answered ]-Delete N random objects Django orm
0
This sounds like a greatest-per-group
problem. Can you try this:
activities = Activity.objects.filter(
id__in=Activity.objects.values('content_type') \
.annotate(max_id=Max('id')) \
.values_list('max_id', flat=True) \
)
- [Answered ]-Purpose of ugettext inside of Models
- [Answered ]-SSL: TLSV1_ALERT_INTERNAL_ERROR when connecting to APNS
- [Answered ]-How to show API description with django_rest_swagger 2.1.1
- [Answered ]-How to use two different email addresses for emails sending?
- [Answered ]-How to limit access to various services per user using Django-mama-CAS?
Source:stackexchange.com