How to implement a ManyToManyField with a View

64 views
Skip to first unread message

Bruce Whealton

unread,
May 26, 2016, 1:43:00 PM5/26/16
to Django users
Hello all,
     I started a similar thread but couldn't find it.
I was creating a Personal Information Management Project, with Project name mypim.  My first app was a contacts app.
This has two Class based Models in the models.py in the contacts directory.  My first view works fine, where I display 
a list of contacts.  It seemed that the Django Admin got confused unless I put the Relationship model prior to the Resource model.
Ok, so, I call a Relationship model instead of Contact model for reasons unimportant to my question.  Here is what my models.py looks like
with some fields snipped to show only what is important.

class Relationship(models.Model):
    category = models.CharField(max_length=60)

    def __str__(self):
        return self.category


class Resource(models.Model):
    first_name = models.CharField(max_length=80)
    last_name = models.CharField(max_length=80)
    organization = models.CharField(max_length=100, null=True, blank=True)
    [snip]
    relationship = models.ManyToManyField(Relationship)

    class Meta:
        ordering = ['last_name',]

    def __str__(self):
        return self.first_name +  " " + self.last_name

And my views.py for the app:

def contact_list(request):
    contacts = Resource.objects.all()
    return render(request, 'contacts/contact_list.html', {'contacts': contacts })


def contact_detail(request, pk):
    contact = get_object_or_404(Resource, pk=pk)
    return render(request, 'contacts/contact_detail.html', {'contact': contact})

End of File.
Ok, so when I display the list of contacts/resources, things look fine and I can click on a contact and see
the details, aka full listing of the contact, minus the categories which come from the Resource.  I know things cannot be 
correct in that the view doesn't seem to query for the categories assigned to the Contact, aka Resource.  My contact_detail.html
template has this section for displaying the category in the Relationship model and I do have defined relationships.

    <section>
      {% for relationship in contact.relationship_set.all %}
        <h3>{{ relationship.category }}</h3>
      {% endfor %}
    </section>

Can someone guide me here, please?  I'd actually like to display the categories separated by commas on the same line.  However, I am not seeing
anything in this area.

Thanks in advance for any help,
Bruce



Simon Charette

unread,
May 26, 2016, 2:03:47 PM5/26/16
to Django users
Hi Bruce,

I think you want to call get_object_or_404 with the Contact model in your
contact_detail() view.

Cheers,
Simon

Bruce Whealton

unread,
May 27, 2016, 7:06:41 PM5/27/16
to Django users
I have that in the contacts/views.py
However, I don't know if it needs the context dictionary to have something which would
connect a contact to many categories.
Thanks,
Bruce

Daniel Roseman

unread,
May 28, 2016, 8:18:32 AM5/28/16
to Django users
The many-to-many field is defined on the Resource model itself, so you use that field itself, rather than any "reverse" relationship syntax.

     {% for relationship in contact.relationship.all %}

(Since that field refers to many objects, you may want to call it "relationships" instead.)
-- 
DR.

Akhil Lawrence

unread,
May 28, 2016, 10:22:01 AM5/28/16
to Django users
Hi Bruce,

You need to call `contact.relationship.all` instead of `contact.relationship_set.all`

We use `_set.all` when we want to retrieve the related items from the master/parent object (Relationship in your case). So if you want to retrieve all contacts under a relationship you can use `<Relationship-obj>.resource_set.all`
Reply all
Reply to author
Forward
0 new messages