The reason I need this is to show activity thread in reverse chronological order. I achieve that by chaining multiple objects into one and then displaying in one place. Currently I have:
contacts = User.contact_created_by.all()
companies = User.company_created_by.all()
tasks = User.tasks_created_by.filter(isComplete=True)
activity = sorted(
chain(contacts, companies, tasks),
key=lambda instance: instance.created_on, reverse=True)
But I need tasks to be sorted by "completion_date" and not by "created_on" date in the resulting object.
I tried this for lambda function:
key=lambda instance: instance.complete_date if (isinstance(instance, Task)) else instance.created_on, reverse=True)
but since "contact" and "company" models do not have "complete_date" fields, I get an error.
Basically the problem is to combine different objects and sort them by different fields (but same type - date!). For example, I further plan to add "event" object to the project which would need to be shown in activity thread, with some type of "event_date".
So the result would be something like the following (in the reverse chronological order):
Today:
-event X happened
-contact X created
-task X completed
Yesterday:
-task Y completed
-company X created
The day before yesterday:
-company Y created