Hello,
I have a problem, when I add a models.DurationField() to an existing Model, then I get an AttributeError with ./manage migrate
return (24 * 60 * 60 * delta.days + delta.seconds) * 1000000 + delta.microseconds
AttributeError: ‘int’ object has no attribute ‘days’
Python 3.7.3
Django==3.2.10
As Database I use default sqlite3
Reproduction:
- Start new project
- Create new app
- Add app to Installed_apps
- Create a Model
class Durtest(models.Model):
name = models.CharField(max_length=20)
- run ./manage makemigrations and ./manage migrate
- Add an DurationField to the existing Model:
from django.db import models
from datetime import timedelta
class Durtest(models.Model):
name = models.CharField(max_length=20)
dur = models.DurationField()
- I run ./manage makemigration.
You are trying to add a non-nullable field 'dur' to durtest 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) Quit, and let me add a default in models.py
I choose 1) and input 0.
- On ./manage migrate again, then I got an error:
File “/********/env/lib/python3.7/site-packages/django/utils/duration.py”, line 44, in duration_microseconds
return (24 * 60 * 60 * delta.days + delta.seconds) * 1000000 + delta.microseconds
AttributeError: ‘int’ object has no attribute ‘days’
- When I change to
dur = models.DurationField(default=timedelta(seconds=0))
the error will show again.
The error does not occur, when the DurationField exists in the initial migration. What make I wrong? Can anyone help?