Django筛选多对多中间表中的多个字段

时间:2022-10-04 19:18:35

I have the following models in my django project:

我在django项目中有以下模型:

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):

现在,我要过滤所有有特定格式的视频,该格式的状态码是10(可以使用)。我怎么做呢?(假设f为格式):

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:

然后,它会返回所有匹配这些假设的视频:

  • a) contain that specific format, and
  • a)包含特定的格式,并且
  • b) contain any format with the status of 10
  • b)包含状态为10的任何格式

How am I supposed to filter only those who have that specific format in a status code of 10?

我怎么能只过滤那些状态码为10的特定格式的用户呢?

thank you!

谢谢你!

3 个解决方案

#1


10  

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)

现在,我要过滤所有有特定格式的视频,该格式的状态码是10(可以使用)。我怎么做呢?(假设f是格式)

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:

这在filter()文档中有记录:

Multiple parameters are joined via AND in the underlying SQL statement.

在底层SQL语句中通过和连接多个参数。

#2


8  

You can chain the filters together for an "AND" construct.

您可以将过滤器链接到“和”构造中。

Videos where the format is f AND the format's status is 10

格式为f,格式状态为10的视频

Video.objects.filter(media__formats=f).filter(media__mediaformat__status=10)

#3


2  

Probably not relevant for the OP, but might be for others like me who found this thread while searching for the right answer.

可能与OP无关,但可能与我这样在寻找正确答案时发现这个线程的人有关。

Ludwik got it right, but the section in the documentation that explains all of this, as well as how to do excludes, is in the queries documentation.

Ludwik是正确的,但是文档中解释所有这些的部分,以及如何排除,都在查询文档中。

Note that splitting the filter into two filter calls like Chris suggested will give you the exact opposite result: it will search for a video that has a media format f and that has a media format, not necessarily the same media format, with a status of 10.

注意,像Chris建议的那样将过滤器分割成两个过滤器调用将会得到完全相反的结果:它将搜索具有媒体格式f和具有媒体格式的视频,不一定是相同的媒体格式,状态为10。

#1


10  

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)

现在,我要过滤所有有特定格式的视频,该格式的状态码是10(可以使用)。我怎么做呢?(假设f是格式)

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:

这在filter()文档中有记录:

Multiple parameters are joined via AND in the underlying SQL statement.

在底层SQL语句中通过和连接多个参数。

#2


8  

You can chain the filters together for an "AND" construct.

您可以将过滤器链接到“和”构造中。

Videos where the format is f AND the format's status is 10

格式为f,格式状态为10的视频

Video.objects.filter(media__formats=f).filter(media__mediaformat__status=10)

#3


2  

Probably not relevant for the OP, but might be for others like me who found this thread while searching for the right answer.

可能与OP无关,但可能与我这样在寻找正确答案时发现这个线程的人有关。

Ludwik got it right, but the section in the documentation that explains all of this, as well as how to do excludes, is in the queries documentation.

Ludwik是正确的,但是文档中解释所有这些的部分,以及如何排除,都在查询文档中。

Note that splitting the filter into two filter calls like Chris suggested will give you the exact opposite result: it will search for a video that has a media format f and that has a media format, not necessarily the same media format, with a status of 10.

注意,像Chris建议的那样将过滤器分割成两个过滤器调用将会得到完全相反的结果:它将搜索具有媒体格式f和具有媒体格式的视频,不一定是相同的媒体格式,状态为10。