Friday, May 27, 2016

Re: How to implement a ManyToManyField with a View

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`



On Thursday, 26 May 2016 23:13:00 UTC+5:30, 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



--
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/ae562e90-a310-4940-9c88-dfb3855a975b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment