Opened 12 months ago

Closed 11 months ago

Last modified 11 months ago

#23365 closed New feature (fixed)

Add support for serializing timezone-aware datetimes in migrations

Reported by: rasca Owned by: rmutter
Component: Migrations Version: 1.7-rc-3
Severity: Normal Keywords: migration
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When adding a new not nullable DateTimeField to a model it prompts you to select a default:

You are trying to add a non-nullable field '...' to user 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)
 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 module is available, so you can do e.g. datetime.date.today()

If you have USE_TZ = True then using datetime.date.today() and executing the migration results in:

RuntimeWarning: DateTimeField ... received a naive datetime (...) while time zone support is active.

I think we should be using:

>>> django.utils.timezone.now
Invalid input: name 'django' is not defined

but it's not possible. I think we should add the django namespace to the prompt and use a different message in case USE_TZ = True

Change History (11)

comment:1 Changed 12 months ago by rasca

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I've set this as a Release Blocker because it leads users to create code that raises a RuntimeWarning.

comment:2 Changed 12 months ago by charettes

What about providing the django.utils.timezone module instead?

comment:3 Changed 12 months ago by andrewgodwin

  • Severity changed from Release blocker to Normal

This is not a release blocker - it does not crash, or cause data loss, it's merely a poor example in an example prompt. Demoting.

That said, we should make the prompt allow for the django timezone-aware stuff to work.

comment:4 Changed 12 months ago by timgraham

  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to New feature

comment:5 Changed 12 months ago by andrewgodwin

  • Easy pickings set

comment:6 Changed 12 months ago by rmutter

  • Owner changed from nobody to rmutter
  • Status changed from new to assigned

comment:7 Changed 12 months ago by rmutter

  • Has patch set

Just created a pull request: https://github.com/django/django/pull/3185

comment:8 Changed 11 months ago by timgraham

  • Easy pickings unset
  • Summary changed from makemigrations with a new not nullable DateTimeField leads to RuntimeWarning to Add support for serializing timezone-aware datetimes in migrations

comment:9 Changed 11 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In a407b846b4c706a54887afaf26b052914601b8b5:

Fixed #23365 -- Added support for timezone-aware datetimes to migrations.

comment:10 Changed 11 months ago by Loic Bistuer <loic.bistuer@…>

In e31be40f163e7d478946530acb1068c6deadb845:

[1.7.x] Fixed #23365 -- Added support for timezone-aware datetimes to migrations.

Backport of a407b846b4 from master

comment:11 Changed 11 months ago by Loic Bistuer <loic.bistuer@…>

In bf1bb07bf02b207f59e7d07243fff367236014d3:

Updated release notes following backport of a407b84. Refs #23365.

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