【问题标题】:multiple values added to database when using save() method in Django在 Django 中使用 save() 方法时将多个值添加到数据库
【发布时间】:2015-02-12 06:29:36
【问题描述】:

我有一个名为“Section”的模型,现在我想更新它的一个属性“fifteenDaysProgressList”,它是一个字符串类型。使用 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

例如,属性“fifteenDaysProgressList”中的原始值为: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。我不知道为什么。

【问题讨论】:

  • 你的目标是什么?你想要一个附加值,还是两个?您能否提供值的示例(以及每种情况下返回的值)?如果你在.save() 之前return.save() 将不会触发,因为调用return 时函数会终止。
  • 我已经更新了我的问题,谢谢~
  • Kevin,你是怎么调用这个函数的?你有没有修改模型的save() 函数?您能否在date_progress[14] = str(thisProgress) 语句之后打印date_progress 的值,在progressStr.rstrip 语句之后打印progressStr 的值?

标签: django django-models save django-views


【解决方案1】:

在我们努力寻找另一种解决方案时,我是否可以建议您研究一下类似以下的内容? (取自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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2016-12-29
    • 2021-04-21
    • 1970-01-01
    • 2020-03-11
    • 2012-06-05
    • 2011-07-24
    相关资源
    最近更新 更多