Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#29595 closed Bug (fixed)

Allow using timedelta in migrations questioner

Reported by: Bas ten Berge Owned by: nobody
Component: Migrations Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (4)

comment:1 by Tim Graham, 6 years ago

Summary: AttributeError: module 'django.utils.datetime_safe' has no attribute 'timedelta' from makemigrationsAllow using timedelta in migrations questioner
Triage Stage: UnreviewedAccepted

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

comment:2 by Tim Graham, 6 years ago

Has patch: set

comment:3 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In c72dde41:

Fixed #29595 -- Allowed using timedelta in migrations questioner.

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

comment:4 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 44accb0:

Refs #32738, Refs #29600, Refs #29595 -- Removed unused django.utils.datetime_safe.time().

Unused since c72dde41e603093ab0bb12fa24fa69cfda0d35f9.

Note: See TracTickets for help on using tickets.
Back to Top