从Django中的多个模型获取数据

从Django中的多个模型获取数据,django,django-models,Django,Django Models,我有3种型号的保险单,CustomerAccount,CustomerAccountYourDetails 政策 客户帐户 CustomerAccountYourDetails 在views.py函数中 在html模板中 以下作品 {% for policy in policies %} {{policy.date}} //gives the date as expected {{policy.customer_account.username}} //gives the username a

我有3种型号的保险单,CustomerAccount,CustomerAccountYourDetails

政策

客户帐户

CustomerAccountYourDetails

在views.py函数中

在html模板中

以下作品

{% for policy in policies %}
{{policy.date}}  //gives the date as expected
{{policy.customer_account.username}} //gives the username as expected
{{need to get first name of customer here}}
{{need to get dob of customer here}}
{% endfor %}
如何从CustomerAccountYourDetails模型(我的第三个模型)中获取第一个\u名称和dob请尝试以下方法

{% for policy in policies %}
{{policy.date}} 
{% for customer in policy.customeraccount_set.all %}
{{customer.name}}
{% for customer_detail in customer.customerdetails_set.all %}
{{customer_detail.dob }}
{% endfor %}{% endfor %}{% endfor %}
这不管用吗{policy.customer\u account.customeraccountyourdetails\u set.all%}
class CustomerAccountYourDetails(models.Model):
    customer_account = models.ForeignKey(CustomerAccount, on_delete=models.CASCADE)    
    first_name = models.CharField("First name", max_length = 30)
    dob = models.DateField("Date of Birth")
policies = Policy.objects.all()
return render(request, 'html file name', 'policies':policies)
{% for policy in policies %}
{{policy.date}}  //gives the date as expected
{{policy.customer_account.username}} //gives the username as expected
{{need to get first name of customer here}}
{{need to get dob of customer here}}
{% endfor %}
{% for policy in policies %}
{{policy.date}} 
{% for customer in policy.customeraccount_set.all %}
{{customer.name}}
{% for customer_detail in customer.customerdetails_set.all %}
{{customer_detail.dob }}
{% endfor %}{% endfor %}{% endfor %}