Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23365 closed New feature (fixed)

Add support for serializing timezone-aware datetimes in migrations

Reported by: rasca Owned by: Rudy Mutter
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 by rasca, 10 years ago

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

comment:2 by Simon Charette, 10 years ago

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

comment:3 by Andrew Godwin, 10 years ago

Severity: Release blockerNormal

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 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted
Type: BugNew feature

comment:5 by Andrew Godwin, 10 years ago

Easy pickings: set

comment:6 by Rudy Mutter, 10 years ago

Owner: changed from nobody to Rudy Mutter
Status: newassigned

comment:7 by Rudy Mutter, 10 years ago

Has patch: set

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

comment:8 by Tim Graham, 10 years ago

Easy pickings: unset
Summary: makemigrations with a new not nullable DateTimeField leads to RuntimeWarningAdd support for serializing timezone-aware datetimes in migrations

comment:9 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In a407b846b4c706a54887afaf26b052914601b8b5:

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

comment:10 by Loic Bistuer <loic.bistuer@…>, 10 years ago

In e31be40f163e7d478946530acb1068c6deadb845:

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

Backport of a407b846b4 from master

comment:11 by Loic Bistuer <loic.bistuer@…>, 10 years ago

In bf1bb07bf02b207f59e7d07243fff367236014d3:

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

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