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