Django / Python新秀。有一个在被叫时没有完成的功能。没有错误,它只是停止并返回到python命令提示符。它执行第一个while循环但在它到达下一个'print'命令之前停止,并将我带回到提示符。执行的一切都是正确的。我确信这是我错过的基本内容。这是功能:
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
答案 0 :(得分:2)
我认为你可能需要在添加腿之后使用.save()你的拼车对象 - 否则腿数可能为零,因为在拼车对象上没有其他数据库命中。这可以解释你的循环跳过。
我认为在循环前腿数是零是正确的吗?打印它,然后在添加腿后尝试.save()拼接对象...
只是预感。