Hi Bruce,

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

Cheers,
Simon

Le jeudi 26 mai 2016 13:43:00 UTC-4, Bruce Whealton a écrit :
>
> 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+unsubscr...@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/b53a4093-12c5-4394-b5cb-ad4dba6a1081%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to