我的播放列表里面有很多歌曲,当我从查询集中获取歌曲时,我希望这些歌曲保持整齐。我尝试过这个显而易见的事情 - 一个'通过'使用order_by =' song_order' - 但是这不会起作用,并试图让它在Song上使用代理模型(参见下面的代码)最终会抛出这个错误:
AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'
目前,这就是它的样子:
class Song(models.Model):
sid = models.AutoField(primary_key=True)
id = models.CharField(max_length=60)
title = models.CharField(max_length=300)
artist_name = models.CharField(max_length=250)
artist_id = models.CharField(max_length=50)
mood = models.ForeignKey('Mood', related_name='songs_with_mood', blank=True, null=True)
def __unicode__ (self):
return "%s - %s" % (self.title, self.id)
class PlaylistSong(Song):
class Meta:
proxy = True
ordering = ['playlist_song_ordering__song_order']
# ^ this is the line responsible for the Attribute Error
class PlaylistSongs(models.Model):
song = models.ForeignKey("PlaylistSong", related_name='playlist_song_ordering')
playlist = models.ForeignKey("Playlist")
song_order = models.IntegerField()
class Meta:
db_table = "api_playlist_songs"
ordering = ['song_order']
class Playlist(models.Model):
owner = models.ForeignKey('UserProfile', related_name='owned_playlists')
title = models.CharField(max_length=70)
songs = models.ManyToManyField('Song', related_name='in_playlists', through='PlaylistSongs')
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-created',)
unique_together = ("owner", "title", "created",)