Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude entire QuerySet from results

I have the following models:

 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)

I'd like to do a query like this:

 banned_songs = BannedSong.objects.filter(lib_entry__player=activePlayer)
 available_songs = LibraryEntry.objects.filter(player=activePlayer).exclude(banned_songs)

Basically if a song is banned, I want to exclude it from my set of available songs. Is there a way to do this in Django?

like image 812
Kurtis Nusbaum Avatar asked Apr 16 '12 23:04

Kurtis Nusbaum


People also ask

How do I exclude multiple objects from a queryset?

def exclude(request): topics = Topics.objects.exclude(title="lorem 2") #print QuerySet print(topics) return HttpResponse(topics) return all objects except that match title="lorem 2". To exclude multiple objects, we'll use in filter with the exclude () method.

How to exclude one object from Django queryset?

Django’s exclude () method basically returns a new QuerySet containing the objects that do not match the given parameter. To exclude one object from Django Queryset follow the given steps:- Start a new Django project. Create a project named exclude by using command line django-admin startproject exclude. This will create a project.

How to exclude queries in SQL?

When the SELECT query runs, the database server applies the restricting condition on each row that is being fetched while searching. If the row meets the mentioned condition, it is included. The basic syntax for writing exclude queries in SQL are as follows: SELECT column_name1, column_name2, ...

How do I exclude multiple objects from a httpresponse?

return all objects except that match title="lorem 2". To exclude multiple objects, we'll use in filter with the exclude () method. def exclude(request): excludes = ['lorem 2', 'lorem 3'] topics = Topics.objects.exclude(title__in=excludes) #print QuerySet print(topics) return HttpResponse(topics)


1 Answers

banned_song_ids = (BannedSong.objects.filter(lib_entry__player=activePlayer)
                                            .values_list('lib_entry', flat=True))

available_songs = (LibraryEntry.objects.filter(player=activePlayer)
                                            .exclude('id__in' = banned_song_ids))

The alternative is:

available_songs = (LibraryEntry.objects.filter(player=activePlayer)
                                          .filter(bannedsong__isnull = True))
like image 110
agf Avatar answered Oct 04 '22 14:10

agf