On 8/16/2010 9:30 PM, Nick wrote: > > Thanks for the reply. > > I assume the problem is coming from the views since the template 'for' > loop is technically doing what it's supposed to. > I find that hard to believe - you appear to be saying that you only want the candidates for the *last* state_race. In which case why bother to compute the candidates for the others?
As far as I can see your code for race in state_races: candidates = race.candidate_set.filter(status="runoff") should give exactly the same result at candidates = state_races[-1].candidate_set.filter(status="runoff") but of course I could be mistaken. Ignoring that for the moment you might want to simplify a little bit - it looks as though you were heading along this road but didn't quite go all the way. racetypes = {"2": "runoff", "3": "general"} # What happened to "1"? def races_output(request, rtyp) # type() is a Python built-in! r = racetypes(rtyp) t = loader.get_template("Government/race_output_%s.html" % rtyp) state_races = Race.objects.select_related().filter(type=rtyp) # then whatever you decide needs to be done with the races Hope this helps. regards Steve > How do I limit the candidate_set(s) for each race to the statuses > mentioned above? I have a very duct taped solution in my template but > it's very unwieldly and confusing (many many if's). Can I move this > filtering to the view to clean up the template? > That's almost certainly the right approach. Whenever your templates get clogged up with logic it indicates you need to migrate the logic to views. This makes your templates much more readable too! > On Aug 16, 7:41 pm, Steve Holden <holden...@gmail.com> wrote: >> 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-9http://djangocon.us/ -- 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.