Saturday, May 28, 2016

Re: How to implement a ManyToManyField with a View

On Thursday, 26 May 2016 18:43:00 UTC+1, Bruce Whealton wrote:
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



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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/76c120d5-f07f-480b-aacd-0786e41f7386%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment