I have the following models in my django project:
class Video(models.Model):
media = models.ForeignKey(Media)
class Media(models.Model):
title = models.CharField(max_length=255)
formats = models.ManyToManyField(Format,through='MediaFormat',related_name='media',blank=True)
class Format(models.Model):
title = models.CharField(max_length=50)
class MediaFormat(models.Model):
status = models.IntegerField()
format = models.ForeignKey(Format)
media = models.ForeignKey(Media)
Now, I want to filter all videos which have a specific format, AND the status code for that format is 10 (ready to use). How can I do that? (assuming that f is the format):
f = Format.objects.get(pk=3)
I'm tempted to use:
Video.objects.filter(media__formats=f, media__mediaformat__status=10)
But then, that would return all videos that matches both of these assumptions:
How am I supposed to filter only those who have that specific format in a status code of 10?
thank you!
Now, I want to filter all videos which have a specific format, AND the status code for that format is 10 (ready to use). How can I do that? (assuming that f is the format)
The code you posted will do exactly what you want:
Video.objects.filter(media__formats=f, media__mediaformat__status=10)
This is documented in the filter()
documentation:
Multiple parameters are joined via AND in the underlying SQL statement.