#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.c44910a6923d093a81ad7f1dcbf720fe%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to