On Apr 5, 8:49 pm, codecowboy <[email protected]> wrote:
> I posted a question earlier today about circular imports (http://
> groups.google.com/group/django-users/t/6119e979131c8c25). I now have
> a follow question to that. I've got the following view logic.
>
> def portal(request):
> scientist_id = 1
> s = get_object_or_404(Scientist, pk=scientist_id)
> return render_to_response('scientists/portal.html',
> {'scientist':s})
>
> Now, Scientists are related (many-to-many) to Conferences through
> ConferenceAttendee. Both of these models are created in a different
> app. I won't paste them in unless someone needs to see them in order
> to answer my question.
>
> I only want to loop through conferences that this scientist is
> presenting. (ConferenceAttendee.presenter=True). I have tried
> following the docs (http://docs.djangoproject.com/en/dev/topics/db/
> queries/#field-lookups-intro), but it seems that you can only call the
> filter method in python files like views.py or models.py. I've tried
> replacing the loop code below with "for ...conference._set.filter(...)
> but python gives me errors when I do that. Here is my template logic.
>
> <h2>Your Presentations</h2>
>
> <ul>
> {% for conference in scientist.conference_set.all %}
> <li><a href="/conferences/{{ conference.id }}">
> {{ conference.title }}</a><a href=""><img src="/site_media/images/
> portal_start_button.jpeg" /></a></li>
> {% endfor %}
>
> </ul>
>
> I've been stuck on this one for days now so any help would be greatly
> appreciated. Please do point me to any articles that I may have
> missed that already answer this post.
>
> Thank you,
>
> Guy
The issue is that in a template, you can't call any method or function
that requires an argument. There are two ways of dealing with this:
create a custom template tag or filter, or create a method that
doesn't need arguments.
In this case, I'd go with the second option. Just create a method on
the Scientist model that returns a queryset of conferences at which
they are a presenter.
class Scientist(models.Model):
.... blah ....
def presenting_conferences(self):
return self.conferences_set.filter(presenter=True)
#or whatever the filter is
and in the template:
{% for conference in scientists.presenting_conferences %}
etc.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---