我如何查询一对多相关对象的字段?

问题描述 投票:0回答:1

因此,如果我有模型Run和ExceutedTestResults

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):中创建一个queryset方法,该方法将返回在一次运行中全部通过/未运行的运行的查询集”>

我不确定如何在一对多关系中做到这一点。建议?

<< [

您可以查询filter中的相关模型以获得所需的结果-运行,运行结果为通过/不运行,就像这样。
Run.objects.filter(run_results__result__in=['P', 'N'])
python django
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.