[Fixed]-TimeField and DurationField int error

1đź‘Ť

âś…

Please check if you are importing timedelta from datetime package 🙂

from datetime import timedelta

These two should work

DurationField(default=timedelta(minutes=20))
DurationField(default=timedelta())

to_python function of DurationField is following

    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

If you are still having troubles with timedelta you could use one of these formats as stated in parse_duration code comments

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

0đź‘Ť

I have quite the same problem, and iklinac’s Answer helped me.

Once I ran the python manage.py makemigrations and showing

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

So I set a 0 to the DurationField cause of laziness…

after that the error keeps showing…

By seen this answer I found that when running “python manage.py migrate” it actually running a 000x_auto_xxxx.py in the folder named migrations. so I found that .py file which contained the field=models.DurationField(default=0)
and change it to the right way, and finally solve the problem.

Hope this may help

👤cjwn

Leave a comment