Django filter through multiple fields in a many-to-many intermediary table

后端 未结 3 925
闹比i
闹比i 2021-02-19 06:15

I have the following models in my django project:

class Video(models.Model):
    media = models.ForeignKey(Media)

class Media(models.Model):
    title = models.         


        
3条回答
  •  無奈伤痛
    2021-02-19 06:31

    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.

提交回复
热议问题