0

我需要做这样的事情:有一个计算、更新字段并保存它的函数。

问题是当我使用 obj = UserSkillblabla.objects.get(pk=1) 获取对象并且我调用 obj.calculate_TotalSkillPoints() 时,它不会将值保存到字段中。

class UserSkillPointProfile(TimeStampedModel):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=_('user'),
                                related_name='skillpoint_profile')
    totalSkillPoints = models.IntegerField(default=0)
    skillPointsAbility1 = models.IntegerField(default=0)
    skillPointsAbility2 = models.IntegerField(default=0)
    skillPointsAbility3 = models.IntegerField(default=0)
    skillPointsAbility4 = models.IntegerField(default=0)

    def calculate_TotalSkillPoints(self):
        awards = AwardAsUser.objects.filter(pk=self.user.pk).exclude(assigned=False)
        result = 0
        for aw in awards:
            if (aw.award.action == "+") or (aw.award.action == "addition"):
                result += aw.award.value
            elif (aw.award.action == "x") or (aw.award.action == "multiplication"):
                result *= aw.award.value
            elif (aw.award.action == "-") or (aw.award.action == "subtraction"):
                result -= aw.award.value
            elif (aw.award.action == "/") or (aw.award.action == "division"):
                result /= aw.award.value
            else:
                pass

            self.totalSkillPoints = result
            self.save()
        return result

我知道有什么问题。也许我需要从这个类中取出函数,但我认为使用 instanceobjetc.function() 来更新值很有用。我没有在 django 参考中找到类似的东西。有人可以给我一些帮助吗?谢谢 :)

4

0 回答 0