如何在 Django 中发送到模板之前替换查询集中的对象?
楷模
class Level(models.Model):
[...]
previous_level = ForeignKey('Level', blank=True, null=True)
class CompletedLevel(models.Model):
user = models.ForeignKey('Auth.user')
level = models.ForeignKey('Level')
result = models.IntegerField()
意见:
levels = Level.objects.filter(previous_level=None)
for i,level in enumerate(modules):
try:
#if level is completed with result > 80 then replace this level with next level
if CompletedLevel.objects.get(result__gt=80):
modules[i] = Module.objects.get(previous_level=level)
except:
pass
收到错误:
'QuerySet' object does not support item assignment