models.py
class PlayLists(models.Model):
name = models.CharField(max_length=256, null=True, blank=True)
created_on = models.DateTimeField(auto_now_add=True)
class PlayListVideos(models.Model):
coach = models.ForeignKey(coachRegister, on_delete=models.CASCADE, related_name='playlist_videos',null=True,blank=True)
video = models.ForeignKey(VideosModel, on_delete=models.CASCADE, related_name='playlists_videos',null=True,blank=True)
playlist = models.ForeignKey(PlayLists, on_delete=models.CASCADE, related_name='playlists_videos',null=True,blank=True)
views.py
from .models import PlayListVideos
from django.http import JsonResponse
from django.db.models import count
def playlists(request):
res = list(PlayListVideos.objects.values('playlist__name','video__url','video__title').annotate(videos=Count('playlist__name')))
return JsonResponse(res, safe=False)
获取结果:
[
{
"video__url": "DX-FIfLb19A",
"videos": 1,
"playlist__name": "game of thrones",
"video__title": "70th Emmy Awards: Peter Dinklage Wins For Outstanding Supporting Actor In A Drama Series"
},
{
"video__url": "rlesc2MYVoA",
"videos": 1,
"playlist__name": "game of thrones",
"video__title": "Game of Thrones 7x05 - Jon Snow meets Drogon - Daenerys reunites with Jorah"
},
{
"video__url": "mk1DXwb-XbM",
"videos": 1,
"playlist__name": "mylist",
"video__title": "Game of Thrones 7x03 - Jon Snow meets Daenerys Targaryen"
}
]
预期结果:
[
{
"video__url":[
"rlesc2MYVoA",
"DX-FIfLb19A"
],
"videos": 2,
"playlist__name": "game of thrones",
"video__title":[
"70th Emmy Awards: Peter Dinklage Wins For Outstanding Supporting Actor In A Drama Series",
"Game of Thrones 7x05 - Jon Snow meets Drogon - Daenerys reunites with Jorah"
]
},
{
"video__url":[
"mk1DXwb-XbM"
],
"videos": 1,
"playlist__name": "mylist",
"video__title":[
"Game of Thrones 7x03 - Jon Snow meets Daenerys Targaryen"
]
}
]
在这里,我希望按照播放列表__name获得不同的结果 而且我仍然使用聚合查询,但依播放列表名称__p
在获取数据时单个模型的独特工作。但这不是 处理相关领域
请看看
答案 0 :(得分:-3)
这是例外使用尝试,除了处理代码或错误处理代码