For that you could use the method select_related, if you want the details
page has executives related to the nonprofit:

e = Executive.objects.select_related().get(pk=id)

Thus, there is a performance gain, it does not require database queries.

2010/12/21 Matt <mizzousunde...@gmail.com>

> I have two classes in my model: nonprofit and executive. It's a one-to-
> many relationship, multiple executives for each nonprofit.
> I created an index page that displays all executives and all
> nonprofits. I've created detail pages for nonprofit and executive.
> But when I try to link back to a nonprofit page on the executive
> detail page, I get weird results and I'm not sure why.
>
> With the view file and template below, my index page stops properly
> linking to all the executives. It stops after the number of nonprofits
> in the database.
> And the executive detail template is still ignoring the nonprofit
> call. What am I missing?
>
> View:
> from django.shortcuts import render_to_response, get_object_or_404
> from nonprofit.models import executive, nonprofit
>
> def index(request):
>        nonprofits = nonprofit.objects.all()
>        executives = executive.objects.all()
>        dictionaries = {'nonprofits': nonprofits, 'executives': executives}
>        return render_to_response('nonprofit/index.html', dictionaries)
>
> def detail(request, id):
>        n = get_object_or_404(nonprofit, pk=id)
>        dictionaries = {'nonprofit': n}
>        return render_to_response('nonprofit/detail.html', dictionaries)
>
> def executive_index(request):
>    executives = executive.objects.all()
>    return render_to_response('nonprofit/executive_index.html',
> {'executives': executives})
>
> def executive_detail(request,id):
>        e = get_object_or_404(executive, pk=id)
>        n = get_object_or_404(nonprofit, pk=id)
>        dictionaries = {'executive': e, 'nonprofit': n}
>        return render_to_response('nonprofit/executive_detail.html',
> dictionaries)
>
>
> template:
> {% load humanize %}
> <h1>{{ executive.name }}</h1>
> <ul>
> Organization: <a href="/nonprofit/{{ nonprofit.id }}">
> {{ executive.nonprofit }}</a><br>
> Title: {{ executive.title }}<br>
> Salary: ${{ executive.salary|intcomma }}
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>


-- 
Att. Mário A. Chaves Neto
Designer / U.I. Engineer
MBA - Design Digital

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to