整個查詢集我有以下型號:排除從結果
class LibraryEntry(models.Model):
player = models.ForeignKey(Player)
player_lib_song_id = models.IntegerField()
title = models.CharField(max_length=200)
artist = models.CharField(max_length=200)
album = models.CharField(max_length=200)
track = models.IntegerField()
genre = models.CharField(max_length=50)
duration = models.IntegerField()
is_deleted = models.BooleanField(default=False)
class Meta:
unique_together = ("player", "player_lib_song_id")
def __unicode__(self):
return "Library Entry " + str(self.player_lib_song_id) + ": " + self.title
class BannedSong(models.Model):
lib_entry = models.ForeignKey(LibraryEntry)
def __unicode__(self):
return "Banned Library Entry " + str(self.lib_entry.title)
我願意做這樣的查詢:
banned_songs = BannedSong.objects.filter(lib_entry__player=activePlayer)
available_songs = LibraryEntry.objects.filter(player=activePlayer).exclude(banned_songs)
基本上如果一首歌曲被禁止,我想排除它來自我的可用歌曲集。有沒有辦法在Django中做到這一點?
你能不能讓 'is_banned' 你LibraryEntry的模型的布爾字段? – jimw 2012-04-16 23:17:47
是的,但實際上禁止的歌曲數量與沒有的歌曲數量相比實際上會被禁止。我想添加一個布爾型字段,大多數情況下只會是一個值是不好的形式。 – 2012-04-16 23:19:40
我不會這麼說,但我認爲這是一個品味問題。 – jimw 2012-04-16 23:21:05