编程语言
首页 > 编程语言> > python-在Django中查询查询集?尝试返回一个步骤,然后返回其下的子步骤

python-在Django中查询查询集?尝试返回一个步骤,然后返回其下的子步骤

作者:互联网

我认为这实际上是非常直观的,但我无法弄清楚.

因此,我有一个名为SWS_Document的模型.然后,我有SWS_Document_Step,它具有SWS_Document的外键.接下来,我有第三个模型SWES_Step,它具有SWS_Document_Step的外键.本质上,SWES_Document_Step是SWS_Document_Step的子步骤.

例.将是“将黄油混入配方中”将是SWS_Document_Step.而SWES_Document_Step__id = 1将是“将黄油放入微波炉安全的碗中”. SWES_Document_Step__id = 2将是“微波黄油30秒”.

这些是将黄油混合到配方中的子步骤.

class SWS_Document(models.Model):

    machines = models.ManyToManyField(Machine, related_name='SWS_documents')
    document_description = models.CharField(max_length=150, default="")
    pub_date = models.DateTimeField(auto_now=True)

class SWS_Document_Step(models.Model):

    STEP_TYPE_CHOICES = (
        ('People', 'People'),
        ('Quality', 'Quality'),
        ('Velocity', 'Velocity'),
        ('Cost', 'Cost'),
    )
    document_number = models.ForeignKey(SWS_Document, on_delete=models.CASCADE)
    sws_sequence_number = models.PositiveIntegerField(editable=True, null=True)

class SWES_Step(models.Model):

    STEP_TYPE_CHOICES = (
        ('People', 'People'),
        ('Quality', 'Quality'),
        ('Velocity', 'Velocity'),
        ('Cost', 'Cost'),
    )
    sws_document_id = models.ForeignKey(SWS_Document_Step, on_delete=models.CASCADE, null=True)
    swes_description = models.CharField(max_length=500)
    swes_step_type = models.CharField(max_length=8, choices=STEP_TYPE_CHOICES, blank=True)
    pub_date = models.DateTimeField(auto_now=True)

因此,在我的view.py中,我采取了所有措施.

def DocumentView(request, document_id):
    # final goal should be to pass a list full of lists...
    # e.g. [
    #[name1, [hazard1, hazard2], [assessment1, assessment2]],
    #[name2, [hazard3, hazard4], [assessment3, assessment4]],
    #]
    steps = []
    swsallsteps = SWS_Document_Step.objects.filter(document_number=document_id)
    swesallsteps = SWES_Step.objects.filter(sws_document_id=document_id)
    return render(request, 'StandardWorkDocuments/document.html', {
        'swsallsteps': swsallsteps,
        'swesallsteps': swesallsteps,
    })

然后在Document.html中,有一些for循环.

{% for step in swsallsteps %}
    <button class='collapsible'>{{ step.sws_sequence_number }} - {{ step.sws_work_element_description}} - <em><b>{{step.sws_step_type}}</b></em> - published - {{step.pub_date}}</button>
    <div class="content">
        {% for swessteps in swesallsteps %}
            <p>{{swessteps.swes_description}}</p>
        {% endfor %}
    </div>
{% endfor %}

本质上,我相信我要获取的是查询中的queryset.这样就可以了[[将黄油混入食谱中,[将黄油放入微波炉安全的碗中,将黄油微波30秒]]

This is what I’m currently getting

This is what I’d hope to get, with a few dumby points put in to show for example

解决方法:

您可以(并且应该)通过父项访问嵌套项. Django文档中有一些关于访问相关对象的有用的examples,在question中,您可以找到有关访问相关对象的更详细的清单.例如,related_name属性可以替换model_set语法并提高可读性(如果选择适当的话).

您的第二个循环应如下所示:

{% for swes_step in step.swes_step_set.all %}
    <p>{{swes_step.swes_description}}</p>
{% endfor %}

现在,您不是从单独的查询集中而是从父步骤访问第二步骤级别.您可以抓取第二个查询集(swesallsteps).

为了避免发出大量的数据库查询,您应该使用prefetch_related以尽可能少的步骤获取所有数据:

swsallsteps = (SWS_Document_Step.objects
    .filter(document_number=document_id)
    .prefetch_related('swes_step_set')
)

标签:django-queryset,django-views,python,django
来源: https://codeday.me/bug/20191210/2105187.html