5

I know this is simple, but I can't get my head around how to join some models together to display in my template in Django. I have "groups" that can have several "contacts".

So far I've got:

class Group(models.Model):
    group_name = models.CharField()

class Contact(models.Model):
    contact_name = models.ForeignKey(Group)

In my view, at first I assumed that simply getting my groups would also get any attached contacts, however that doesn't appear to be happening as expected:

def get_queryset(self):
    groups = Group.objects.all()
    return groups

I was expecting to do something like this in my template:

{% for group in groups %}
    <h2>{{ group.group_name }}</h2>
    {% for c in group.contact %}
        <h3>{{ c.contact_name }}</h3>
    {% endfor %}
{% endfor %}

This isn't working - what am I doing wrong? What is the correct query in my view to make sure the contact(s) for each group is getting retrieved?

3
  • 1
    {% for c in group.contact_set.all %} Commented Oct 13, 2013 at 5:58
  • As stated in this part of the doc: docs.djangoproject.com/en/1.5/ref/models/relations Commented Oct 13, 2013 at 6:59
  • Thanks Burhan, the way I was reading the docs, I thought the .contact_set method was to be used in the view, I didn't even think to use it in the template like that. Appreciated :) Commented Oct 13, 2013 at 7:44

1 Answer 1

8

Well, it looks like you've got some of your code from a different place so just so you can fully understand, you can do this in 2 different ways:

1) To access a related object of any kind, being a simple ForeignKey or ManyToMany you just need to go from the opposite model and use _set like this example:

class Group(models.Model):
    group_name = models.CharField()

class Contact(models.Model):
    contact_name = models.ForeignKey(Group)

{{ group.contact_set.all }}

2) You can set up a name different than the default _set changing Contact like this:

class Contact(models.Model):
    contact_name = models.ForeignKey(Group, related_name='contacts')

So, related_name kwarg set a new name for you instead of the _set one:

{{ group.contacts.all }}

I hope I manage to make it clearer about simple access on models related objects.

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, thanks! I had read about the "_set" method, but didn't realise it was to be used in the template. I thought I must have to use it in the view before I started looking at the template...! Thanks for your great answer.
Yes, you can use it anywhere to access the related object. No problem, I'm glad I could help. :)
Hi, i know is been a long time since you have answered this question. I am still new to django and i am facing the same problem i tried solution 1 in my models.py but i keep getting 'ForeignKey' object has no attribute contact_set
It looks like you're already calling your foreign key object. Can you share your code here? Otherwise, try changing contact_set to all()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.