2đ
I donât think you want to do any database level aggregation, because you presumably want to show the individual details as well as the counts e.g. âX like 5 photosâ and show the 5 photos. Aggregation, by definition, will exclude the individual data.
Instead, you should do the grouping and sorting in Python code (or Javascript since I think youâre using an HTTP API, but I would prefer a server side API that had things already organised).
itertools.groupby might help. I think you need to group by (user and action), and then sort by the timestamp of the first item in each group, so that you might see âJoe liked 5 photosâ, âAnne posted 2 photosâ, âJoe posted a photoâ, âClaire liked 3 photosâ etc.
1đ
In your feed resource, you are already overriding get_object_list, I would suggest to change the logic to perform a raw query for aggregation logic.
def get_object_list(self, request):
query = "Your aggregation query logic here"
feed_model = self._meta.object_class
return feed_model.objects.raw(query)
This should do what is required. However, you need to think of your query logic. Let me know if you face any other problem.
Thanks!
- Django + uWSGI + Nginx + SSL â request for working configuration (emphasis on SSL)
- Django 1.6 and django-registration: built-in authentication views not picked up
0đ
I think the best way to do this would be to modify the Activity table to store grouped activities. When a new action happens, check for existing ones of the same type and either edit the record to make it âgroupedâ, or add a new record. You could add ManyToMany relationships to all potential table that contain related records, or just store the data in a json field that contains enough information to render the activity in the feed without making queries to other table objects.
If itâs too resource intensive, you can queue the adding/editing of new activities. Youâre probably better off keeping the activity table just a straight feed that doesnât require any processing when rendered. Itâs not the easiest solution to implement, but I think that in the long run it makes sense.
- Make browser submit additional HTTP-Header if click on hyperlink
- Convert Python None to JavaScript null
- Why does Django South require a default value when removing a field?
- Django sub-applications & module structure
0đ
Set the aggregation time, for example do you want to aggregate all âlikesâ within a 10 minute period or for the past 24 hours?
Then you can filter your object by this time frame.
Then you can apply grouping using the .values(âmodel__fieldâ) method. Django generates sql that contains âGROUP BYâ
And then finally add an aggregation limit so that when the number of likes is over this limit you display the aggregated view rather than the single activity view.
Example below (pseudo, not actual):
if (activity.object.filter(time__gt=yourlowertime, time__lt=youruppertime).values(âactivity__action').count() > aggregation_limit) :
# show aggregated view
else:
# show normal view
- Python multiple inheritance function overriding and ListView in django
- How to filter filter_horizontal in Django admin?