码迷,mamicode.com
首页 > 其他好文 > 详细

django 动态更新属性值

时间:2016-04-24 11:03:52      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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)  # 添加闯关时间

 

.

django 动态更新属性值

标签:

原文地址:http://www.cnblogs.com/weiok/p/5426433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!