打印一个以上家庭的人员列表,每个家庭有多个电话号码 [英] printing a list of persons with more than one home, each home with more than one phone number
问题描述
我已经定义了类,但现在我试图创建一个视图,列出每个人,其所有的家庭和所有的电话号码为每个家庭住址...类似于:
约翰·史密斯
123假货str
305-99-8877
305-99-8876
321橡木路
444-98-7654
peter guy
453 north ave ...
到目前为止,我有这样的东西:
(在我的views.py上)
def ViewAll请求):
people = Person.objects.all()
render_to_response('viewall.html',{'people':people})
(和我的模板)
{%人民币%}
{{guy.name}}
{%if person.home_address_set.all%}
{{home_address}}
{%for ?? ????在???? %}
#print每个房屋中的电话号码
{%endfor%}
{%endif%}
{%endfor%}
任何想法如何写我失踪?当然,如果有另一种方式(更好的更优雅或更有效的方式)做我需要的,我很乐意听到。
你有什么似乎是三个嵌套的集合:人,家庭,电话号码。
步骤1 - 你如何在视图函数中写这个?
for p in Person.objects.all():
printperson,p
for h在p.home_address_set.all()中:
打印home,h
在h.phone_set.all()中的ph:
打印phone,ph
不要忽略这一步。如果您无法使其在视图功能中工作,您的模型是错误的。花费时间来获得此部分。
步骤2 - 将其转换为模板语法。
{%for p on people%}
{%for h in p.home_address_set.all%}
{%fpr ph in h.phone_set.all%}
{%endfor%}
{%endfor%}
{%endfor%}
结果应与您的查看功能相同。
I have a class Person which can have several Homes, each one with one or many Phone numbers.
I have defined the classes, but now i am trying to create a view wich list every person, with all its homes and all the phone numbers for each home address... something like:
john smith
123 fake str
305-99-8877
305-99-8876
321 oak road
444-98-7654
peter guy
453 north ave...
so far i have something like this:
(on my views.py)
def ViewAll(request):
people = Person.objects.all()
render_to_response('viewall.html', {'people': people})
(and on my template)
{% for guy in people %}
{{ guy.name }}
{% if person.home_address_set.all %}
{{ home_address }}
{% for ?????? in ???? %}
#print phone numbers in each home
{% endfor %}
{% endif %}
{% endfor %}
any idea of how to write the for I'm missing? of course, if there is another way (a better more elegant or efficient way) of doing what I need, I would love to hear it.
You have what appears to be three nested collections: Person, Home, Phone Number.
Step 1 - How would you write this in a view function?
for p in Person.objects.all():
print "person", p
for h in p.home_address_set.all():
print " home", h
for ph in h.phone_set.all():
print " phone", ph
Don't omit this step. If you can't make it work in a view function, your model is wrong. Take the time to get this part right.
Step 2 - Convert this into template syntax.
{% for p on people %}
{% for h in p.home_address_set.all %}
{% fpr ph in h.phone_set.all %}
{% endfor %}
{% endfor %}
{% endfor %}
The results should be the same as your view function.
这篇关于打印一个以上家庭的人员列表,每个家庭有多个电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!