Django 在 For 循环中的过滤器中使用变量

Django Using Variable in Filter within For Loop

我正在尝试使用父对象获取子 object/field 值。 parent 是 for loop 中的一个变量,我似乎无法将它交给 custom tag.

#custom_tags.py
@register.simple_tag()
def assigned_to(sample):
    #sample=sample.pk
    return Lab_Request.objects.filter(sample=sample).first().lab.lab_name

@register.filter()
def assigned_too(sample):
    #sample=sample.pk
    return Lab_Request.objects.filter(sample=sample).first().lab.lab_name
#sample.html
{% for sample in samples %}

                {% static sample.0|assigned_too %}

                {% if user.profile.employee.pk == sample.inspector.employee.pk or perms.ics.view_sample %}

                                    <tr>
                                        <td class="column1">{{ sample.sample_date }}</td>
                                        <td class="column2">{{ sample.status|sample_status_options }}</td>
                                        <td class="column3"><a href="#">{{ sample.sample_number }}</a></td>
                                        <td class="column4"><a href="#">{{ sample.order.customer.customer_name }}</a></td>
                                        <td class="column5"><a href="#">{{ sample.lab_request.placeholder_to_be_replaced }}{{ sample.lab_request.lab.lab_name }}{{ sample.inspection_request.inspector.employee.employee_first_name }}</a></td>
                                        <td class="column6">{{ sample.date_modified|date:'M. d, Y'  }}</td>
                                    </tr>
                {% endif %}
            {% endfor %}

{% static sample|assigned_too %} 是我苦苦挣扎的部分。我还尝试编写一个函数并像 {% assigned_to {{ sample }} %} 那样调用它。如果我使用 {% static 1|assigned_too %} 它确实有效,但它不会像它需要的那样迭代我的循环。我不确定我是否正在以这种最复杂的方式进行操作。我只想要来自父对象的子对象的信息,例如 {{ sample.lab_request.lab.lab_name }},其中 sample 是父对象,lab_request 是子模型。

编辑:

#views.py
class SampleHomeView(ListView):
    model = Sample
    samples = Sample.objects.all
    context_object_name = 'samples'
    template_name = 'ics/sample.html'
    ordering = ['-sample_date']
    paginate_by = 10
#urls.py
path('sample/', SampleHomeView.as_view(), name='sample-home'),
#models.py

class Lab_Request(models.Model):
    #Add stuff here
    placeholder_to_be_replaced = models.CharField(max_length=1)
    lab = models.ForeignKey(Lab, on_delete=models.CASCADE, null=True, blank=True)
    sample = models.ForeignKey(Sample, on_delete=models.CASCADE)

class Sample(models.Model):

    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    status = models.CharField(max_length=2, choices=SAMPLE_STATUS_OPTIONS, default="01")

class Order(models.Model):

    order_number = models.CharField(max_length=19, unique=True, editable=False, default=get_order_number)
    status = models.CharField(max_length=2, choices=ORDER_STATUS_OPTIONS)
    specification = models.ForeignKey(Specification, on_delete=models.CASCADE, null=True, blank=True) #Needs work to determine which spec is appropriate
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

class Lab(models.Model):

    status = models.CharField(max_length=2, choices=STATUS_OPTIONS)
    lab_name = models.TextField(max_length=100, unique=True)

好的,我明白了。我需要将 child 调用为 parent 与我所做的略有不同。我需要使用 {{ sample.lab_request_set.all.0.lab }} 而不是 {{ sample.lab_request.lab }}

对于将来需要此功能的任何人,如果您没有在 ForeignKey 中设置相关名称,那么它将是 parent_model_name.child_model_name_set.all 这将为您提供一个查询集,您可以循环访问该查询集,或者只是 select 第一个就像我在 .all 末尾对 .0 所做的一样。这适用于模板。如果您在 python 文件中需要它,那么您将使用 parent_model_name.child_model_name_set.all()

调用该函数