[Django] #29595: AttributeError: module 'django.utils.datetime_safe' has no attribute 'timedelta' from makemigrations

34 views
Skip to first unread message

Django

unread,
Jul 25, 2018, 10:09:43 AM7/25/18
to django-...@googlegroups.com
#29595: AttributeError: module 'django.utils.datetime_safe' has no attribute
'timedelta' from makemigrations
-----------------------------------------+------------------------
Reporter: Bas ten Berge | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I created a model that contains a `models.duration` field:

{{{

class VoiceMessage(models.Model):
....
duration = models.DurationField(help_text='Contains the sample duration')
...
}}}

It's not a nullable field, so makemigrations correctly asks me for a
default value:

{{{
(env) bastb@bastb-vps:/var/www/brownpapersession/dev/brownpapersession$
python manage.py makemigrations
You are trying to add a non-nullable field 'duration' to voicemessage
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
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can
do e.g. timezone.now
Type 'exit' to exit this prompt
>>> datetime.timedelta(seconds=1)
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/core/management/__init__.py", line 371, in
execute_from_command_line
utility.execute()
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/core/management/commands/makemigrations.py", line 159, in
handle
migration_name=self.migration_name,
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/db/migrations/autodetector.py", line 44, in changes
changes = self._detect_changes(convert_apps, graph)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/db/migrations/autodetector.py", line 183, in
_detect_changes
self.generate_added_fields()
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/db/migrations/autodetector.py", line 824, in
generate_added_fields
self._generate_added_field(app_label, model_name, field_name)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/db/migrations/autodetector.py", line 844, in
_generate_added_field
field.default = self.questioner.ask_not_null_addition(field_name,
model_name)
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/db/migrations/questioner.py", line 158, in
ask_not_null_addition
return self._ask_default()
File "/var/www/brownpapersession/dev/env/lib/python3.5/site-
packages/django/db/migrations/questioner.py", line 138, in _ask_default
return eval(code, {}, {"datetime": datetime_safe, "timezone":
timezone})
File "<string>", line 1, in <module>
AttributeError: module 'django.utils.datetime_safe' has no attribute
'timedelta'
}}}

To me, the informative message `The datetime and django.utils.timezone
modules are available, so you can do e.g. timezone.now` suggests that the
python `datetime` module is available, especially because the Django
`timezone` was displayed as `django.utils.timezone`.

I'm working around it by accepting a null and blank value for now. I'm
happy to create a PR if needed

--
Ticket URL: <https://code.djangoproject.com/ticket/29595>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 25, 2018, 6:56:14 PM7/25/18
to django-...@googlegroups.com
#29595: Allow using timedelta in migrations questioner
-------------------------------+------------------------------------

Reporter: Bas ten Berge | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

I guess it might be a matter of adding the `timedelta` import in
`django.utils.datetime_safe`.

--
Ticket URL: <https://code.djangoproject.com/ticket/29595#comment:1>

Django

unread,
Jul 26, 2018, 4:07:53 PM7/26/18
to django-...@googlegroups.com
#29595: Allow using timedelta in migrations questioner
-------------------------------+------------------------------------
Reporter: Bas ten Berge | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/10230 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29595#comment:2>

Django

unread,
Jul 27, 2018, 12:19:33 PM7/27/18
to django-...@googlegroups.com
#29595: Allow using timedelta in migrations questioner
-------------------------------+------------------------------------
Reporter: Bas ten Berge | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"c72dde41e603093ab0bb12fa24fa69cfda0d35f9" c72dde41]:
{{{
#!CommitTicketReference repository=""
revision="c72dde41e603093ab0bb12fa24fa69cfda0d35f9"
Fixed #29595 -- Allowed using timedelta in migrations questioner.

Refs #29600 -- Removed usage of django.utils.datetime_safe in migrations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29595#comment:3>

Django

unread,
May 12, 2021, 9:43:49 AM5/12/21
to django-...@googlegroups.com
#29595: Allow using timedelta in migrations questioner
-------------------------------+------------------------------------
Reporter: Bas ten Berge | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 2.0

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"44accb066a51d15f27e38f203c73830eddea16df" 44accb0]:
{{{
#!CommitTicketReference repository=""
revision="44accb066a51d15f27e38f203c73830eddea16df"
Refs #32738, Refs #29600, Refs #29595 -- Removed unused
django.utils.datetime_safe.time().

Unused since c72dde41e603093ab0bb12fa24fa69cfda0d35f9.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29595#comment:4>

Reply all
Reply to author
Forward
0 new messages