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 out all the videos with a specific format, and the status code for this format is 10 (ready to use). How can i do this? (assuming f is a format):
f = Format.objects.get(pk=3)
I am tempted to use:
Video.objects.filter(media__formats=f, media__mediaformat__status=10)
But then it will return all videos that match both of these assumptions:
- a) contain this particular format and
- b) contain any format with status 10
How can I filter only those with this format in status code 10?
Thank you!
django django-orm many-to-many
user684334
source share