#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 , 7 years ago
| Summary: | AttributeError: module 'django.utils.datetime_safe' has no attribute 'timedelta' from makemigrations → Allow using timedelta in migrations questioner |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
Note:
See TracTickets
for help on using tickets.
I guess it might be a matter of adding the
timedeltaimport indjango.utils.datetime_safe.