考虑这种模型结构:
class Playlist(models.Model):
track_list = models.ManyToManyField('Track', blank=True, through='TrackToPlaylist')
class Track(models.Model):
artist = models.CharField(max_length=128)
class TracktoPlaylistManager(models.Manager):
def get_ordered_tracks(self):
return sorted(self.get_queryset(), key=lambda x: x.votes_balance, reverse=True)
class TracktoPlaylist(models.Model):
track = models.ForeignKey(Track)
playlist = models.ForeignKey(Playlist)
objects = TracktoPlaylistManager()
@property
def votes_balance(self):
# here's order calculated
return
我想通过模型的管理器访问以获取播放列表对象的排序曲目。但是Playlist.objects.first()。track_list是一个ManyToManyFieldManager对象。 Playlist.objects.first().track_list.through.objects
返回此管理器,但我必须按播放列表过滤实例...这是双重工作。