On 8/16/2010 5:40 PM, Nick wrote:
> I have a view that handles elections/races. That view takes a request
> object and then based on that object sends the information to a
> specific template.
> 
> For each election/race there is a subset of candidates based on the
> initial candidate_set from that race. I would like to know how to
> filter that set in a view. Here is what I have so far:
> 
> Models.py:
> 
> Candidate
>     name = charfield
>     id = integer
>     race = ForeignKey(Race)
> 
> Race
>     name = charfield
>     type = integerfield
> 
> Views.py
> 
> def races_output(request, type): # the type is a number that equates
> to general (3), runoff (2) or primary (1)
>     if type == 2:
>          r = "runoff"
>          t = loader.get_template("Government/race_output_2.html")
>          state_races = Race.objects.select_related().filter(type=type)
>          for race in state_races:
>             candidates = race.candidate_set.filter(status="runoff")
> 
>     if type == 3:
>          r = "general"
>          t = loader.get_template("Government/race_output_3.html")
>          state_races = Race.objects.select_related().filter(type=type)
>          for race in state_races:
>             candidates = race.candidate_set.filter(status="general")
> 
>     c = Context({'state_races': state_races, 'candidates': candidates,
> 'r':r})
>     html = t.render(c)
> 
> template
> 
> {% for race in state_races %}
> 
> {{race.name}} - {{race.type}}
> 
> {% for cand in candidates %}
> {{ cand.name }} - {{cand.id }}
> {% endfor %}
> 
> {% endif %}
> 
> The problem is the candidate for loop returns the exact same
> candidates for every single race. How do I get it so that the
> candidates query is specific to the race in the for loop?
> 
First of all, note that you are setting candidates in a for loop. So
what you see is only the result of the last iteration of that loop - the
results of all previous iterations are being overwritten by the last one.

regards
 Steve
-- 
DjangoCon US 2010 September 7-9 http://djangocon.us/

-- 
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