0

Django/Python rookie. Have a function that isn't finishing when called. No error, it just stops and goes back to the python command prompt. It does the first while loop but stops before it gets to the next 'print' command, and takes me back to the prompt. Everything that executed is correct. I'm sure it's something basic I've missed. here's the function:

def create_carpool(family, start_date, end_date, name):
    carpool = Carpool()
    carpool.name = name
    carpool.save()
    current_date = start_date

    while current_date <= end_date:
        print 'Processing ' + current_date.strftime('%m/%d/%Y') + ' going to ' + end_date.strftime('%m/%d/%Y')
        if current_date.day not in (5,6):
            start_leg = Leg()
            start_leg.drive_date = current_date
            start_leg.start_time = START_TIME_AM
            start_leg.endpoint = ENDPOINT_AM
            start_leg.save()
            end_leg = Leg()
            end_leg.drive_date = current_date
            end_leg.start_time = START_TIME_PM
            end_leg.endpoint = ENDPOINT_PM
            end_leg.save()
            carpool.legs.add(start_leg)
            carpool.legs.add(end_leg)
        current_date += datetime.timedelta(days=1)

    print 'Adding driving preferences for all carpool legs.'
    num_legs = carpool.legs.all().count()
    leg_idx = 0
    for leg in carpool.legs.all():
        leg_idx += 1
        print 'Processing leg #' + str(leg_idx) + ' of ' + str(num_legs)
        for rider in Rider.objects.all():
            if choice(number_list) >= 4:
                leg.riders.add(rider)
        for driver in carpool.drivers.all():
            drive_preference = DrivingPreference()
            drive_preference.leg = leg
            drive_preference.driver = family.drivers
            drive_preference.preference = choice([1,2,3])
            drive_preference.save()
    return carpool
1
  • 1
    Have you debugged the code to see what's contained in each collection when you start the loops? Perhaps something is not getting added and the collections simply have no more elements to iterate through? Commented Nov 10, 2011 at 14:46

1 Answer 1

2

I think you may need to .save() your carpool object after adding legs to it - otherwise the leg count might be zero because there wont have been another database hit on the carpool object. That would explain your loop skipping.

Am I right in thinking the leg count is zero before the loop? Print it, then try .save()ing the carpool object after adding the legs...

Just a hunch.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. carpool.save() let the loop finish. Now I have to fix the code so that it finishes correctly and does what it's supposed to do! Thanks for the help!
@mb52089: Welcome to StackOverflow. If this answer has solved your problem, I recommend you accept it by clicking on the check mark outline. This will reward Spacedman for his correct answer, give you a couple of reputation points, and encourage people to answer your future questions.
sniff thanks Fred. All I want is to get to 10k before the end of the year... Maybe... maybe...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.