Django根据字段(createdtstamp)

时间:2016-12-22 10:06:57

标签: django django-queryset mergesort

我有三个 django模型,我想获得CRUD日志。根据对象创建日期(createdtstamp字段),我有merged个查询集和sorted个查询集。我在每次创建/更新/删除时都使用 django-simple-history 存储Django模型状态。

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)

以上合并和排序似乎不起作用!

如何根据createdtstamp字段对查询结果进行排序(按最新排序)?

之前已经提出过类似的问题 Display objects from different models at the same page according to their published date

但我的问题不同,因为Player.history.select_related没有Coach and Title个字段。

Player.history.select_related('Coach', 'Title').all().order_by('-createdtstamp')

0 个答案:

没有答案