M
M
Max2019-08-23 08:20:04
Django
Max, 2019-08-23 08:20:04

How to write data to DurationField in Django 2.2?

Good afternoon
There is a model

class Calls(models.Model):
    time = models.DurationField(null=True, blank=True, verbose_name='Продолжительность')

There is an .xls file from which we take a Cell with a value:
00:20:15
Next, create a new record:
duration = row[2] #Строка из файла
Calls.objects.create( time = duration)

I am getting an error:
'str' object has no attribute 'days'
Exception Location: ...\django\utils\duration.py in duration_microseconds, line 44

I tried to convert the string to:
'datetime.datetime' object
'float' object

But still they do n't have the ' days ' attribute

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2019-08-23
@Acaweb

I decided

import datetime as datetime_class # забыл добавить в ответ

duration = datetime.strptime(duration, "%H:%M:%S")
x_duration =datetime_class.timedelta(hours=duration.hour, minutes=duration.minute, seconds=duration.second).total_seconds()	
duration = timedelta(seconds = x_duration)

A
alternativshik, 2019-08-23
@alternativshik

he needs timedelta, not a string, not a date, not a number, but timedelta

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question