下面是我的代码:
def release_it(request, id):
if request.method == "POST":
x = Lap.objects.get(pk=id)
x.released = True
p = request.POST["release_time"]
initial_time = datetime.strptime(p, '%Y-%m-%dT%H:%M')
x.release_time = initial_time
x.save()
load = Loaded.objects.filter(lap=id).update(release_time=initial_time)
for l in load:
l.save()
return render(request, "race/index.html", {
"message":"released!",
"x":x,
"load":load
})
return render(request, "race/index.html", {
"message":"released!"
})
我得到这个错误:
File "/Volumes/John HDD/Visual Studio Code/CS50-web-Programming-with-Python-and-JavaScript/Project/pigeon_race/race/views.py",第27行,在release_it中对于l的负载:TypeError: 'int' object is not iterable
,但当我替换load = Loaded.objects.filter(lap=id).all()
。它工作得很好
class Loaded(models.Model):
race_id = models.CharField(max_length=64)
lap = models.CharField(max_length=64)
lap_name = models.CharField(max_length=64)
pigeon_id = models.CharField(max_length=65)
pigeon_name = models.CharField(max_length=64)
pigeon_ring = models.CharField(max_length=64, blank=True)
pigeon_hcode = models.CharField(blank=True, max_length=64)
release_time = models.CharField(blank=True, max_length=64)
clock_time = models.CharField(blank=True, max_length=64)
Update返回由查询更新的记录的数目。(不完全更新,但匹配一个过滤器子句-更多信息)您可以在更新后再次查询记录。
Loaded.objects.filter(lap=id).update(release_time=initial_time)
load = Loaded.objects.filter(lap=id).all()