Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django TimeField和DurationField int错误_Django_Django Models_Django 1.10 - Fatal编程技术网

Django TimeField和DurationField int错误

Django TimeField和DurationField int错误,django,django-models,django-1.10,Django,Django Models,Django 1.10,在运行python manage.py migrate时,我遇到了这个错误。在使用DurationField和TimeField时,两者都给了我一个: return int(round(value.total_seconds() * 1000000)) AttributeError: 'int' object has no attribute 'total_seconds' 我在这个网站上看到过这个错误,但只在Django 1.8上看到过。目前,我正在使用Django 1.10。 我试过这些建

在运行python manage.py migrate时,我遇到了这个错误。在使用DurationField和TimeField时,两者都给了我一个:

return int(round(value.total_seconds() * 1000000))
AttributeError: 'int' object has no attribute 'total_seconds'
我在这个网站上看到过这个错误,但只在Django 1.8上看到过。目前,我正在使用Django 1.10。 我试过这些建议:

DurationField(default=timedelta()), 
DurationField(), 
DurationField(default=timedelta()), 
DurationField(default=int(timedelta(minutes=20).total_seconds())). 
我的模型当前看起来像:

class SomeModel(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    lunchnumber = models.IntegerField(null=True, blank=True)
    role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES,null=True, blank=True)
    breaktime = models.DurationField(default=timedelta(minutes=45))

    def to_python(self, value):
        if value is None:
            return value
        if isinstance(value, datetime.timedelta):
            return value
        try:
            parsed = parse_duration(value)
        except ValueError:
            pass
        else:
            if parsed is not None:
                return parsed
解决Prakar Trivedi的问题: 我不知道什么价值观不起作用。我假设这是django试图填充该数据库表的默认值。我之所以有这样的印象是因为 ……在get_db_prep_值中 返回整数(四舍五入(值.总秒数()*1000000)) AttributeError:“int”对象没有“total_seconds”属性

针对iklinac的回答:


我进入并添加了默认值=timedelta(分钟=45)。我确实从datetime导入了它,但我觉得我遗漏了一些东西。我对这一点非常陌生,还没有见过to_python函数。我遗漏了什么?我仍然收到相同的错误?

请检查是否从datetime包导入timedelta:)

这两种方法应该有效

DurationField(default=timedelta(minutes=20))
DurationField(default=timedelta())
以下是DurationField的python函数

    if value is None:
        return value
    if isinstance(value, datetime.timedelta):
        return value
    try:
        parsed = parse_duration(value)
    except ValueError:
        pass
    else:
        if parsed is not None:
            return parsed
如果您在timedelta方面仍然有问题,您可以使用parse_duration code注释中所述的其中一种格式

def parse_duration(value):
    """Parses a duration string and returns a datetime.timedelta.

    The preferred format for durations in Django is '%d %H:%M:%S.%f'.

    Also supports ISO 8601 representation.
    """
    match = standard_duration_re.match(value)
    if not match:
        match = iso8601_duration_re.match(value)
    if match:
        kw = match.groupdict()
        if kw.get('microseconds'):
            kw['microseconds'] = kw['microseconds'].ljust(6, '0')
        kw = {k: float(v) for k, v in six.iteritems(kw) if v is not None}
        return datetime.timedelta(**kw)

我也有同样的问题,iklinac的回答帮助了我

一旦我运行了python manage.py makemigrations并显示

You are trying to change the nullable field 'duration' on task to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
 3) Quit, and let me add a default in models.py
所以我将DurationField的懒惰原因设置为0

之后,错误会继续显示

通过查看此答案,我发现在运行“python manage.py migrate”时,它实际上在名为migrations的文件夹中运行000x_auto_xxxx.py。因此我找到了包含字段=models.DurationField(默认值=0)的.py文件 并将其改为正确的方式,最终解决问题


希望这可能会有帮助

什么是价值中的价值。总秒数()。哪一个字段是你的模型的价值??哦@PrakharTrivedi这就是我想问的,似乎我问了其他问题:)嘿,特拉维斯,你检查过你有没有按照我在你的models.py中所述的方式导入timedelta。另外,关于_python函数,在Duration字段后面的函数不需要包含它。关于注释的最后一部分,如果datetime仍然存在问题,您可以使用下面parse_ValueComment部分中所述的django time格式,这样您就可以设置下面所述格式的default='timestring'
You are trying to change the nullable field 'duration' on task to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
 3) Quit, and let me add a default in models.py