Template: Foreign Key

1,437 views
Skip to first unread message

russelson

unread,
Jun 30, 2009, 8:43:15 AM6/30/09
to Django users
Hi,

I'm new to django

I have two models

#models.py

class House(models.Model):
name = models.CharField(blank=True, max_length=50)

class People(models.Model):
name = models.CharField(blank=True, max_length=50)
house = models.ForegnKey(House)

#views.py
def house_list(request):
houses = House.objects.all()
return render_to_response('test/house_list.html', {'object_list':
houses})

#house_list.html
{% for object in object_list %}
{{ object.name }}
{{ object.people.name }} #!?!?
{% endfor %}

How do i retrieve the information in the People model?

Please advice

Thanks in advance

Kenneth Gonsalves

unread,
Jun 30, 2009, 12:00:42 PM6/30/09
to django...@googlegroups.com
On Tuesday 30 June 2009 14:13:15 russelson wrote:
> #models.py
>
> class House(models.Model):
> name = models.CharField(blank=True, max_length=50)
>
> class People(models.Model):
> name = models.CharField(blank=True, max_length=50)
> house = models.ForegnKey(House)
>
> #views.py
> def house_list(request):
> houses = House.objects.all()
> return render_to_response('test/house_list.html', {'object_list':
> houses})
>
> #house_list.html
> {% for object in object_list %}
> {{ object.name }}
> {{ object.people.name }} #!?!?
> {% endfor %}
>
> How do i retrieve the information in the People model?

object.people_set.all() will give you the list of people in the house
--
regards
kg
http://lawgon.livejournal.com

Fady Kamal

unread,
May 18, 2012, 11:50:36 PM5/18/12
to django...@googlegroups.com
it's not working with me

Kurtis Mullins

unread,
May 19, 2012, 2:50:33 AM5/19/12
to django...@googlegroups.com
Try something along these lines (Note: I'm switching up your variable names a bit to make it easier to read)

{% for house in houses %}
    {{ house.name }}
    {% for person in house.people.all %}
        {{ person.name }}
    {% endfor %}
{% endfor %}

I just wrote that code block pretty quickly but it should step you in the right direction. Also, here's a related stackoverflow post: http://stackoverflow.com/questions/1014591/traversing-foreign-key-related-tables-in-django-templates

Good lucK!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/piLnSG9ycFEJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Kurtis Mullins

unread,
May 19, 2012, 2:51:23 AM5/19/12
to django...@googlegroups.com
Whoops -- that might need to be 'house.people_set.all'. Sorry.

Fady Kamal

unread,
May 19, 2012, 4:55:34 PM5/19/12
to django...@googlegroups.com
actually it worked even without the the set.all ,thanks for help
Reply all
Reply to author
Forward
0 new messages