I have three django models and I want to get CRUD logs. I have merged
queryset and sorted
them based on object creation date (createdtstamp field). I am using django-simple-history stores Django model state on every create/update/delete.
player = Player.history.all().values('first_name','history_date', 'history_id', 'history_type', 'history_user_id', 'id', 'createdtstamp')
coach = Coach.history.all().values('coach_name','history_date', 'history_id', 'history_type', 'history_user_id', 'id', 'createdtstamp')
title = Title.history.all().values('title_name', 'history_date', 'history_id', 'history_type', 'history_user_id', 'id', 'createdtstamp')
#merge queryset and sort based on createdtstamp
result_list = sorted(chain(player, coach, title), key=lambda instance: instance.createdtstamp)
queryjson = json.dumps(result_list, cls=DjangoJSONEncoder)
return HttpResponse(queryjson)
Above merge and sort doesn't seem to work!
How can I sort query result based on createdtstamp field (sort by latest)?
Similar question have been asked before Display objects from different models at the same page according to their published date
But my question is different as Player.history.select_related
doesn't have Coach and Title
fields.
Player.history.select_related('Coach', 'Title').all().order_by('-createdtstamp')
.