def get_random_song_list(self, n):
# ?????n?QueryObject??,??QuerySet
# ???????order_by('?')
result = []
count = self.aggregate(count=Count('id'))['count']
for _ in range(n):
try:
random_index = randint(0, count - 1)
except ValueError:
raise MusicLibrary.DoesNotExist
result.append(self.all()[random_index])
return result
评论列表
文章目录