我有以下代表歌曲和每首歌曲播放的模型:from django.db import modelsclass Play(models.Model): play_day = models.PositiveIntegerField() source = models.CharField( 'source', choices=(('radio', 'Radio'),('streaming', 'Streaming'), ) ) song = models.ForeignKey(Song, verbose_name='song') class Song(models.Model): name = models.CharField('Name')圖像我有以下條目:歌曲:|ID | name ||---|---------------------|| 1 | Stairway to Heaven || 2 | Riders on the Storm |播放:|ID | play_day | source | song_id ||---|----------|-----------|---------|| 1 | 2081030 | radio | 1 || 1 | 2081030 | streaming | 1 || 2 | 2081030 | streaming | 2 |我想列出所有曲目如下:| Name | Day | Sources ||---------------------|------------|------------------|| Stairway to Heaven | 2018-10-30 | Radio, Streaming || Riders on the Storm | 2018-10-30 | Streaming |我正在使用Django==1.9.2,django_tables2==1.1.6和django-filter==0.13.0PostgreSQL。問題: 我使用 Song 作為表和過濾器的模型,因此查詢集以 select 開頭FROM song。但是,在加入 Play 表時,我在“天堂的階梯”的情況下獲得了兩個條目(我知道,即使一個也太多了:https : //www.youtube.com/watch?v=RD1KqbDdmuE)。我試過的:我嘗試為 Song 設置一個 distinct ,盡管這會導致我無法對 Song.id 以外的其他列進行排序的問題(假設我在該列上做了 distinct)聚合:這會產生一個最終狀態,實際上是一個字典,不能與 django_tables 一起使用。我找到了這個解決方案,用于 PostgreSQL選擇按某些列排序并在另一列上不同的行,盡管我不知道如何使用 django 執行此操作。問題: 使用 Django 的 ORM 從引用中“聚合”信息的每行顯示一條軌道的正確方法是什么?
添加回答
舉報
0/150
提交
取消