【发布时间】:2020-03-31 07:39:10
【问题描述】:
现在我有以下,非常慢但有效的代码:
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
它返回一个字典,其中歌曲 ID 用作字典键,整数值列表用作值。前三个键如下:
crossover_list = {
1:[38,37],
2:[38],
....
}
这里有没有人知道将其包装成单个查询的简洁方法?数据存在于具有三列的单个表中,其中每个 song_id 可以与多个 dance_id 相关联。
song_id | playlist_id | dance_id
1 1 38
1 2 37
2 1 38
理想情况下,我想弄清楚如何返回是:
<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)
【问题讨论】:
-
我怀疑你想要
prefetch_related。但是展示你的模型。
标签: django django-queryset annotate