如何 return Django 注释中的列表?

How do I return a list in a Django Annotation?

现在我有以下非常慢但有效的代码:

crossover_list = {}
    for song_id in song_ids:
        crossover_set = list(dance_occurrences.filter(
            song_id=song_id).values_list('dance_name_id', flat=True).distinct())
        crossover_list[song_id] = crossover_set

它return是一个字典,其中歌曲 ID 用作字典键,整数值列表用作值。前三个键如下:

crossover_list = {
        1:[38,37],
        2:[38],
        ....
}

这里有人知道将其打包成单个查询的简洁方法吗?数据存在于具有三列的单个 table 中,其中每个 song_id 可以与多个 dance_ids.

相关联
song_id | playlist_id | dance_id
      1             1         38
      1             2         37
      2             1         38

理想情况下,我想弄清楚如何 return 是:

<QuerySet[{'song_id':1, [{'dance_id':38, 'dance_id':37}]}, {'song_id':2, [{'dance_id':38}]}]>

如有任何想法或帮助,我们将不胜感激。

编辑:根据要求,这里是有问题的模型:

# This model helps us do analysis on music/dance crossover density, song popularity within-genre,
# playlist viability within-genre (based on song occurrence counts per song within each playlist), etc.
class SongOccurrences(models.Model):
    song = models.ForeignKey(
        'Songs',
        on_delete=models.CASCADE,
    )
    playlist = models.ForeignKey(
        'Playlists',
        on_delete=models.CASCADE,
    )

    dance_name = models.ForeignKey(
        'DanceMasterTable',
        on_delete=models.CASCADE,
    )

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['song', 'playlist'], name='unique occurrence')
        ]

# This model contains relevant data from Spotify about each playlist
class Playlists(models.Model):
    spotify_playlist_uri = models.CharField(max_length=200, unique=True)
    spotify_playlist_owner = models.CharField(max_length=200)
    spotify_playlist_name = models.CharField(max_length=200)
    current_song_count = models.IntegerField()
    previous_song_count = models.IntegerField()
    dance_name = models.ForeignKey(
        'DanceMasterTable',
        on_delete=models.CASCADE,
    )

# This model contains all relavent data from Spotify about each song
class Songs(models.Model):
    title = models.CharField(max_length=200)
    first_artist = models.CharField(max_length=200)
    all_artists = models.CharField(max_length=200)
    album = models.CharField(max_length=200)
    release_date = models.DateField('Release Date', blank=True)
    genres = models.CharField(max_length=1000, blank=True)
    popularity = models.FloatField(blank=True)  # This value changes often
    explicit = models.BooleanField(blank=True)
    uri = models.CharField(max_length=200, unique=True)
    tempo = models.FloatField(blank=True)
    time_signature = models.IntegerField()
    energy = models.FloatField(blank=True)
    danceability = models.FloatField(blank=True)
    duration_ms = models.IntegerField()
    tonic = models.IntegerField(blank=True)
    mode = models.IntegerField(blank=True)
    acousticness = models.FloatField(blank=True)
    instrumentalness = models.FloatField(blank=True)
    liveness = models.FloatField(blank=True)
    loudness = models.FloatField(blank=True)
    speechiness = models.FloatField(blank=True)
    valence = models.FloatField(blank=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['title', 'first_artist', 'all_artists'], name='unique song')
        ]

# This model contains the (static) master list of partner dances to be analyzed.
class DanceMasterTable(models.Model):
    dance_name = models.CharField(max_length=200, unique=True)

您在循环中进行 运行 查询,因此速度很慢。

您可以事先通过所有 song_ids 筛选 dance_occurrences,最后循环遍历这些值以将舞蹈 ID 附加到它们各自的歌曲 ID。

示例

song_dance_occurrences = dance_occurrences.filter(
    song_id__in=song_ids
).values_list('song_id', 'dance_id').distinct()

crossover_dict = {}
for song_id, dance_id in song_dance_occurrences:
    crossover_dict[song_id] = crossover_dict.get(song_id, [])
    crossover_dict[song_id].append(dance_id)