每次我使用 .update(release_time=initial_time) 时,我都会收到一个错误'int'对象不可迭代,但如果我将其更改为 .all() 它就可以正常工作



下面是我的代码:

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()

相关内容

  • 没有找到相关文章

最新更新