django 动态更新属性值
class TestTime(models.Model): """ 功能说明:指定测试的每一关总用时 """ test = models.ForeignKey(Test, verbose_name=u"主测试ID") level_1 = models.IntegerField(u"关卡1用时", default=0) level_2 = models.IntegerField(u"关卡2用时", default=0) level_3 = models.IntegerField(u"关卡3用时", default=0) level_4 = models.IntegerField(u"关卡4用时", default=0) level_5 = models.IntegerField(u"关卡5用时", default=0) level_6 = models.IntegerField(u"关卡6用时", default=0) level_7 = models.IntegerField(u"关卡7用时", default=0) level_8 = models.IntegerField(u"关卡8用时", default=0) objects = TestTimeManager() class Meta: db_table = "pxb_test_time" class TestTimeManager(models.Manager): """ 功能说明:TestTime 扩展 """ def set_time(self, test_id, level_id, use_time): """设置闯关时间""" if not str(use_time).isdigit(): return use_time = int(use_time) test_time = self.filter(test=test_id) if test_time: test_time = test_time[0] else: test_time = TestTime(test_id=test_id) test_time.save() # kwargs = {'level_id': F("level_id") + use_time} used_time = getattr(test_time, level_id, 0) use_time += used_time kwargs = {level_id: use_time} # 设置动态参数 self.filter(test=test_id).update(**kwargs) lever_id = 3 use_time = 30 TestTime.objects.set_time(test_id, "level_%s" % level_id, use_time) # 添加闯关时间
.