This appears to be working. Thanks a lot Steve, I really appreciate
the help.

On Aug 17, 9:07 am, Nick <nickt...@gmail.com> wrote:
> Thanks, Steve. I'll implement this today and see what happens.
>
> On Aug 16, 9:46 pm, Steve Holden <holden...@gmail.com> wrote:
>
> > On 8/16/2010 10:30 PM, Nick Tankersley wrote:> Thank you again for your 
> > replies. They've been full of usefull
> > > information about how to clean up an otherwise pretty sloppy bit of code.
>
> > No problem. I hope you don't mind me keeping this dialog on the list (I
> > see you didn't copy django-users, perhaps not realizing that a "Reply
> > All" would be required to do so).
>
> > > By the way, type 1 races passed about a month ago so it doesn't come
> > > into play.
>
> > Thanks! That cleared up a mystery.
>
> > > I can see how you misunderstand my goal. After I get the initial query
> > > for the races I want to filter each races candidate_set to include only
> > > those candidates with a specific status. That is where I am getting stuck.
>
> > > What I meant by saying that the template for loop is working properly is
> > > that based on my flawed query logic it is returning exactly what is
> > > should, a list of candidates in the last entry.
>
> > Ah! Penny drops! I wasn't really looking at the templates. So what you
> > are really asking is how can you make the template work for every race,
> > and not just the last one?
>
> > A solution I have used in the past is to add attributes to the top-level
> > objects that I can then use in the template. This would in your case
> > involve doing something like
>
> >     for race in state_races:
> >         race.cands = race.candidate_set.filter(status="runoff")
>
> > in the view. Then the template would look like this:
>
> > {% for race in state_races %}
>
> > {{race.name}} - {{race.type}}
>
> > {% for cand in race.cands %}
> > {{ cand.name }} - {{cand.id }}
> > {% endfor %}
>
> > {% endif %}
>
> > I am pretty sure this isn't the recommended way to proceed, but with
> > luck someone on the list will advise us as to what is ...
>
> > regards
> >  Steve
>
> > > On Aug 16, 2010 9:21 PM, "Steve Holden" <holden...@gmail.com
> > > <mailto:holden...@gmail.com>> wrote:
> > >> 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
> > > <mailto: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 <http://race.name>}} - {{race.type}}
>
> > >>>>> {% for cand in candidates %}
> > >>>>> {{ cand.name <http://cand.name> }} - {{cand.id <http://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-9http://djangocon.us/
>
> > --
> > DjangoCon US 2010 September 7-9http://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