在Django中使用save()方法时,多个值被添加到数据库中

时间:2014-12-13 18:47:30

标签: django django-models save django-views

我有一个名为" Section"现在我想更新它的一个属性" fifteenDaysProgressList",它是一个String类型。在我使用save()方法之后,会将多个值添加到String中,但如果我在使用save()之前返回该属性,则只会添加一个值。所以我猜这个问题存在于save()中。以下是我的代码。

def storeFifteenDaysData(project, developer):
thisSection = Section.objects.get(project = project, developer = developer)
thisProgress = thisSection.progress

progressList = str(thisSection.fifteenDaysProgressList)
date_progress = progressList.split()

if(len(date_progress)<15):
    date_progress.append(str(thisProgress))
else:
    for i in range(14):
        date_progress[i] = date_progress[i+1]
    date_progress[14] = str(thisProgress)
# return len(date_progress)
# update the fifteenDaysProgressList
progressStr = ""
for i in range(len(date_progress)):
    progressStr += str(date_progress[i])
    progressStr +=" "
progressStr.rstrip()
thisSection.fifteenDaysProgressList = progressStr
# return thisSection.fifteenDaysProgressList, one value added
thisSection.save()
# return thisSection.fifteenDaysProgressList, multiple values added

class Section(models.Model):
name = models.CharField(max_length = 100)
description = models.CharField(max_length = 1000, null = True, blank = True)
percentage = models.FloatField('the percentage in the the project')
progress = models.FloatField(default = 0)
expectedProgress = models.FloatField(default = 0)
fifteenDaysProgressList = models.CharField(max_length = 200, null = True, blank = True)
developer = models.ForeignKey(Developer, null = True, blank = True)
project = models.ForeignKey(Project, null = True, blank = True)

def __unicode__(self):
    return self.name

例如,属性中的原始值&#34; fifteenDaysProgressList&#34;是:0.1 0.2 0.3, 现在我想在字符串中添加0.4,即我想得到0.1 0.2 0.3 0.4,但在使用save()之后,我总是得到0.1 0.2 0.3 0.4 0.4 0.4。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

当我们努力寻求另一种解决方案时,我是否可以建议您查看以下内容? (从this question检索)

class ListField(models.TextField):
    __metaclass__ = models.SubfieldBase
    description = "Stores a python list"

    def __init__(self, *args, **kwargs):
        super(ListField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        if not value:
            value = []

        if isinstance(value, list):
            return value

        return ast.literal_eval(value)

    def get_prep_value(self, value):
        if value is None:
            return value

        return unicode(value)

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_db_prep_value(value)

class Section(models.Model):
    name = models.CharField(max_length = 100)
    description = models.CharField(max_length = 1000, null = True, blank = True)
    percentage = models.FloatField('the percentage in the the project')
    progress = models.FloatField(default = 0)
    expectedProgress = models.FloatField(default = 0)
    fifteenDaysProgressList = ListField() # update this line with new model field.
    developer = models.ForeignKey(Developer, null = True, blank = True)
    project = models.ForeignKey(Project, null = True, blank = True)

现在你可以将你的进度列表视为普通的Python列表,这样可以更容易维护,并且使用类似

之类的东西更容易保持15个元素以下
thisList = thisSection.fifteenDaysProgressList
while len(thisList) > 15:
    thisList.pop(0)