我没有得到相关领域的明显结果

问题描述 投票:0回答:1

models.朋友

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.朋友

    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"
        ]
      }
    ]

在这里我期待根据playlist__name得到不同的结果,并且我使用了聚合查询仍然没有根据playlist__name得到明显的值

获取数据时单个模型的独特工作。但它不适用于相关领域

请看一看

django
1个回答
-3
投票

这是异常使用尝试除了envent处理或错误处理您的代码

© www.soinside.com 2019 - 2024. All rights reserved.