I have run into this error when running python manage.py migrate when using DurationField and TimeField both give me a:
return int(round(value.total_seconds() * 1000000))
AttributeError: 'int' object has no attribute 'total_seconds'
I have seen this error on this site but only for a Django 1.8. Currently, I am using Django 1.10. I have tried these suggesions :
DurationField(default=timedelta()),
DurationField(),
DurationField(default=timedelta()),
DurationField(default=int(timedelta(minutes=20).total_seconds())).
My model currently looks like:
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
Addressing Prakhar Trivedi's question: I do not know what value is not working well. I was under the assumption it was the default value that django is trying populate that database table with. I am under this impression because of .......in get_db_prep_value return int(round(value.total_seconds() * 1000000)) AttributeError: 'int' object has no attribute 'total_seconds'
Addressing iklinac's Answer:
I went in and added the default=timedelta(minutes=45). I do have it imported from datetime But I feel I am missing something. I am very new at this and have not seen a to_python function. What am I missing I am still getting the same error?