【发布时间】:2015-12-25 19:27:12
【问题描述】:
我有一个模型类
class PlayerScore(models.Model):
number=models.IntegerField()
points=models.FloatField(default=0)
player=models.ForeignKey('Player')
我想对 number 字段的相同值的数据进行分组。 例如。如果 db 中的数据是这样的
number | points | player_id
-------+--------+----------
1 | 45 | 1
1 | 68 | 2
2 | 79 | 3
2 | 70 | 4
我的查询应该返回
[{"number":1, "records": [{"points":45,"player_id":1},{"points":68,"player_id":2}]},
{"number":2, "records" :[{"points":79,"player_id":3},{"points":70,"player_id":4}]}]
请建议如何为此编写查询??
【问题讨论】:
标签: django python-3.x django-models