如果我从数据库中获取所有对象,然后使用 python foreach 循环遍历它们,然后我删除循环中的所有对象。仍作为客户对象在内存中的已删除数据库条目会发生什么情况
for cust in Customer.objects.all():
#delete all
for cust2 in Customer.objects.all():
cust2.delete()
#what is cust now??
cust.save() # is this valid?
是否可以立即刷新每个客户的每次迭代中的值?因此,在处理 cust1 时,我删除了 cust2,然后在下一次迭代中我不开始处理 cust2 ...想象一下:
for cust in Customer.objects.all():
if cust.pay_everyone():
for cust2 in Customer.objects.all():
cust2.paid = True
cust2.save()
#when cust2 comes in the loop, cust2.paid is still False
#the previous pay_everyone() method is undone
if cust.kill_everyone():
for cust2 in Customer.objects.all():
cust2.delete()
#when cust2 comes in the loop, he is still alive when he should be dead
cust.save()
# in cust2's turn in the loop, he has been just resurrected which is not possible