【发布时间】:2019-09-11 21:59:19
【问题描述】:
还有:how to group model data based on same values in django
还有 2:Group serializer results based on value
像上面的其他人一样,我需要以特定方式返回的结果。我想将包含一列相似值的结果组合在一起。
如上所述,我想接受这个:
album | title | singer
-------+-------------------+----------
1 | exampleSongTitle | someone
1 | exampleSongTitle | sometwo
2 | exampleSongTitle | somethree
2 | exampleSongTitle | someone
得到这个
[
{
"album": 1,
"tracks": [
{
"title": exampleSongTitle,
"singer": someone
},
{
"title": exampleSongTitle,
"singer": sometwo
}
]
},
{
"album": 2,
"tracks": [
{
"title": exampleSongTitle,
"singer": somethree
},
{
"title": exampleSongTitle,
"singer": someone
}
]
}
]
我的模特:
class Songs(models.Model):
title = models.CharField(db_column='Title', max_length=400, blank=True, null=True)
album = models.CharField(db_column='Album', max_length=400, blank=True, null=True)
singer = models.CharField(db_column='Singer', max_length=400, blank=True, null=True)
class Meta:
managed = False
db_table = 'Songs'
专辑值将是来自同一专辑的曲目的相同值。
【问题讨论】:
-
你能分享你的模型吗?
-
刚刚更新了模型问题
-
您是否考虑过添加
Album模型和Singer模型并为这些模型设置外键?它会让这种事情变得更简单
标签: django django-rest-framework