I've set up a little history audit trail (django-simple-history) for a few of my key models and an admin view to paginate the (aggregated) entries which are sorted by descending date. The problem is, the method I'm using is suboptimal...
historical_foo = Foo.history.all()
historical_bar = Bar.history.all()
historical_qux = Qux.history.all()
#sort the aggregate by modified date
result_list = sorted(chain(historical_foo, historical_bar, historical_qux), key=attrgetter('history_date'), reverse=True)
paginator = Paginator(result_list, 100)
try:
result = paginator.page(page_num)
#...
This surely won't scale well as this these tables gets large. Is there a way to push the aggregating and sorting logic down into Django / DB or an alternative approach for the same outcome?