I'm under Django 1.8. I have several playlists that contain each several videos. some of these videos have a status of deleted or error.
I want only playlists that contain video with a status of "online".
class Video(models.Model):
# Title of the video
title = models.CharField(max_length=100)
# Status of video (error, online, deleted)
status = models.CharField(max_length=20, default="online")
class UserPlaylist(models.Model):
# Name of the playlist
name = models.CharField(max_length=500, blank=True, null=True)
# Playlist owner
owner = models.ForeignKey(User)
# videos
videos = models.ManyToManyField(Video, null=False)
What I tryed with no success:
UserPlaylist.objects.filter(videos__status="online")
UserPlaylist.objects.filter(videos__status="online").distinct()
Works but painfull:
UserPlaylist.objects.exclude(videos__status='error').exclude(videos__status='deleted').distinct()
We can count the number of
Videos withstatus='online', and check if that is the same as the number of total videos, like:This will yield a query that looks like:
Note that a
UserPlaylistthat contains no videos will be part of the result as well, since all its videos are online.