Django-搜索为每个id创建的最后一个对象,并将总和与其他字段隔开

Django-搜索为每个id创建的最后一个对象,并将总和与其他字段隔开,django,Django,我正在做一个查询,我想得到所有游戏的列表,与一个特定的团队有关,但我想字段“date_created”只保存最后一个日期 我的问题是: "games": [ { "id": 3, "first_name": "Odille", "last_name": "Adamovitz", "date_created": "2017-08-24T00:00:00", "p

我正在做一个查询,我想得到所有游戏的列表,与一个特定的团队有关,但我想字段“date_created”只保存最后一个日期

我的问题是:

 "games": [
        {
            "id": 3,
            "first_name": "Odille",
            "last_name": "Adamovitz",
            "date_created": "2017-08-24T00:00:00",
            "points": "10",
        },
        {
            "id": 3,
            "first_name": "Odille",
            "last_name": "Adamovitz",
            "date_created": "2017-09-18T00:00:00",
            "points": "10",
        },
        {
            "donation__sponsor": 3,
            "first_name": "Odille",
            "last_name": "Adamovitz",
            "date_created": "2016-06-20T00:00:00",
            "points": "10",
        },
        {
            "id": 5,
            "first_name": "Bail",
            "last_name": "Brownbill",
            "date_created": "2017-11-10T00:00:00",
            "points": "10",
        },
        {
            "id": 5,
            "first_name": "Bail",
            "last_name": "Brownbill",
            "date_created": "2018-01-31T00:00:00",
            "points": "10",
        }
]
我的愿望是:

 "games": [
        {
            "id": 3,
            "first_name": "Odille",
            "last_name": "Adamovitz",
            "date_created": "2017-09-18T00:00:00",
            "points": "10",
        },
        {
            "id": 5,
            "first_name": "Bail",
            "last_name": "Brownbill",
            "date_created": "2018-01-31T00:00:00",
            "points": "10",
        }
]
我的最终目标是使用另一个字段进行查询,该字段对其中的属性求和 大概是这样的:

 "games": [
        {
            "id": 3,
            "first_name": "Odille",
            "last_name": "Adamovitz",
            "date_created": "2017-08-24T00:00:00",
            "points": "30",
        },
        {
            "id": 5,
            "first_name": "Bail",
            "last_name": "Brownbill",
            "date_created": "2018-01-31T00:00:00",
            "points": "20",
        }
]
以下是我最贴切的想法:

#I get the sum of points with the name, but missing last date_created
qs = Game.filter(id=1).values('first_name', 'last_name', 'date_created').annotate(points = Sum('points')
使用.latest()我只得到最后一个游戏,而不是与每个id相关的最后一个游戏。 我想我需要做两个查询集,然后某种形式的联合,但是idk

试试这个,

from django.db.models import Sum, Max

Game.objects.values('id').annotate(points=Sum('point'), date_created=Max('date_created'))

此表单相当于原始sql,

Select id,SUM(point) as points, MAX(date_created) as date_created from GameTable Group By id

太棒了,帮我拿一下Max()。我把它和我要找的箱子一起用,谢谢。