所以,如果我有模型 Run 和 ExceulatedTestResults
class Run(TimeStampedModel):
RUN_TYPE_CHOICES = (
(MANUAL_RUN, 'Manual'),
(AUTOMATED_RUN, 'Automated'),
)
run_type = models.CharField(max_length=1, choices=RUN_TYPE_CHOICES)
class ExecutedTestResult(Duration):
"""Lists the results of every test case executed."""
run = models.ForeignKey(Run, on_delete=models.CASCADE,
related_name='run_results')
RESULT_TYPE_CHOICES = (
('P', 'Passed'),
('F', 'Failed'),
('N', 'No Run'),
('B', 'Blocked'),
)
result = models.CharField(max_length=1, choices=RESULT_TYPE_CHOICES)
我想在 class RunQuerySet(QuerySet):
中创建一个查询集方法,该方法将返回一次运行中全部通过/未运行的运行查询集
我不确定如何在一对多关系中执行此操作。有建议吗?
最佳答案
您可以通过filter
中的相关模型进行查询,得到您想要的结果——运行,运行结果为通过/未运行,如下所示。
Run.objects.filter(run_results__result__in=['P', 'N'])
class RunQuerySet(QuerySet):
def passed_or_no_run(self):
return self.filter(run_results__result__in=['P', 'N'])
关于python - 如何查询一对多相关对象的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311932/