We have a class in our models.py:
class Score(model.Model):
score = models.IntegerField()
We want to call the score object and update it inside of our views.py, when an answer is correct:
def answer(request, level_id):
# next three lines are working for us.
o = Level.objects.get(id=level_id)
guess = request.GET.get('guess', '').strip()
correct = o.answers.filter(value__iexact=guess).exists()
b = Score.objects.get('score')
b.score += o.points
b.save()
We still haven't made b.score += o.points run using an if
statement, because first I wanted to see if we can update b.score.
class Level(model.Models):
points = models.IntegerField("Point Value')
The error we get is:
ValueError at /answer/1 too many values to unpack
Try this!
def answer(request, level_id):
# next three lines are working for us.
o = Level.objects.get(id=level_id)
guess = request.GET.get('guess', '').strip()
correct = o.answers.filter(value__iexact=guess).exists()
b = Score.objects.get(score)
b.score += int(o.points)
b.save()
You are missing on saving the object, when you update it.
b.save() # should save it
You would have to specify which Score instance you are trying to query.
Assuming you had created one before, and it has primary key 1, you can access it using:
b = Score.objects.get(pk=1)
Alternatively, if it's the first time, you could run:
b, created = Score.objects.get_or_create(pk=1)
created
will be true if you just created it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.