如何查询除已经在另一个模型中的模型对象之外的所有模型对象?
How to query all model objects except the ones that already are in another model?
我正在开发 Django 应用程序。我有 2 个与问题相关的模型:
class Quiz(models.Model):
"""
Represents a Quiz for a `Module`.
It will have a `name`
"""
name = models.CharField(max_length=200)
user = models.ManyToManyField('cme.Bussines', related_name='quizes', through='UserQuiz', through_fields=('quiz', 'user'))
def __str__(self) -> str:
return f'{self.name}'
class Trio(models.Model):
"""
Represents the content for a Module.
Each `Trio` will have, as it's name says, 3 content fields, plus the
`Module` it belongs to.
It will have a `file`, a `video` (which will be a URL to a YT video), and a `quiz`
(which will be key to `Quiz`)
"""
file = models.FileField(upload_to='legacy/classes/', max_length=254)
quiz = models.ForeignKey(Quiz, on_delete=models.SET_NULL, related_name='trios', null=True, blank=True)
video = models.URLField()
module = models.ForeignKey(Module, on_delete=models.CASCADE, related_name='trios')
user = models.ManyToManyField('cme.Bussines', related_name='trios', through='UserTrio', through_fields=('trio', 'user'))
def __str__(self) -> str:
return f'Trio {self.id} de {self.module}'
我想查询不在Trio
的quiz
字段中的所有Quiz
zes。有办法吗?
是的,您可以查询:
Quiz.objects.filter(<strong>trios=None</strong>)
这将创建一个 LEFT OUTER JOIN 并且只保留 Quiz
zes 没有相关的 Trio
对象。
您可以查询在任何 Trio
对象中看到其 id
的所有 Quiz
对象:
Quiz.objects.exclude(id__in=Trio.objects.values("quiz_id"))
您还可以进一步优化查询。例如,假设您想要所有 Quiz
对象未被 Trio
对象的特定 子集 引用,您可以通过在排除表达式中添加过滤器来实现,类似于:
Quiz.objects.exclude(id__in=Trio.objects.filter(...).values("quiz_id"))
我正在开发 Django 应用程序。我有 2 个与问题相关的模型:
class Quiz(models.Model):
"""
Represents a Quiz for a `Module`.
It will have a `name`
"""
name = models.CharField(max_length=200)
user = models.ManyToManyField('cme.Bussines', related_name='quizes', through='UserQuiz', through_fields=('quiz', 'user'))
def __str__(self) -> str:
return f'{self.name}'
class Trio(models.Model):
"""
Represents the content for a Module.
Each `Trio` will have, as it's name says, 3 content fields, plus the
`Module` it belongs to.
It will have a `file`, a `video` (which will be a URL to a YT video), and a `quiz`
(which will be key to `Quiz`)
"""
file = models.FileField(upload_to='legacy/classes/', max_length=254)
quiz = models.ForeignKey(Quiz, on_delete=models.SET_NULL, related_name='trios', null=True, blank=True)
video = models.URLField()
module = models.ForeignKey(Module, on_delete=models.CASCADE, related_name='trios')
user = models.ManyToManyField('cme.Bussines', related_name='trios', through='UserTrio', through_fields=('trio', 'user'))
def __str__(self) -> str:
return f'Trio {self.id} de {self.module}'
我想查询不在Trio
的quiz
字段中的所有Quiz
zes。有办法吗?
是的,您可以查询:
Quiz.objects.filter(<strong>trios=None</strong>)
这将创建一个 LEFT OUTER JOIN 并且只保留 Quiz
zes 没有相关的 Trio
对象。
您可以查询在任何 Trio
对象中看到其 id
的所有 Quiz
对象:
Quiz.objects.exclude(id__in=Trio.objects.values("quiz_id"))
您还可以进一步优化查询。例如,假设您想要所有 Quiz
对象未被 Trio
对象的特定 子集 引用,您可以通过在排除表达式中添加过滤器来实现,类似于:
Quiz.objects.exclude(id__in=Trio.objects.filter(...).values("quiz_id"))