views:

68

answers:

3

Hi all, I have this code:

msgs = int(post['time_in_weeks'])
for i in range(msgs):
    tip_msg = Tip.objects.get(week_number=i)

it always results in an error saying that no values could be found.

week_number is an integer field. When I input the value of i directly, the query works.

When i print out the value of i I get the expected values.

Any input at all would be seriously appreciated.

Thanks.

A: 

The range function will give you a zero based list of numbers up to and excluding msgs. I guess there is no Tip with week_number=0.

stefanw
There is a Tip with week_number=0.
Even when I use i+1 instead of i still have the same problem. Ultimately trying to get just a distinct value for tip_msg. This all runs on GAE via django non_rel: http://bitbucket.org/wkornewald/django-nonrel/wiki/Home All other queries still work.
A: 

You were right.

The problem wasn't i, it was the value of msgs. Kept coming out at a much larger value than it should have been.

Many Thanks.

A: 

Also, to limit the number of queries you could do this:

for tip in Tip.objects.filter(week_number__lt=msgs):
    #do something

or, if you want specific weeks:

weeks=(1,3,5)
for tip in Tip.objects.filter(week_number__in=weeks):
    #do something
Till Backhaus
Thanks for this! Its a much better way I hadn't even thought of.