Opened 6 years ago
Closed 6 years ago
#30337 closed Uncategorized (worksforme)
Django Admin - Migrations: default value for models.DateTime() with auto_add_now=True
Reported by: | Jilles Soeters | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hello everyone,
In a personal project I created a model, populated the database and later realised that I needed a created
field on it. So I added models.DateTime() with auto_add_now=True. When running the migrations I got this prompt:
`
You are trying to add the field 'created' with 'auto_now_add=True' to squad without a default; the database needs something to populate existing rows.
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
You can accept the default 'timezone.now' by pressing 'Enter' or you can provide another value.
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
[default: timezone.now] >>>
You are trying to add the field 'created' with 'auto_now_add=True' to squadapplication without a default; the database needs something to populate existing rows.
Now I had to run it again and do timezone.now() myself
`
I believe that the default should be timezone.now()
(invoking the function). When the default is timezone.now
- without invoking it - it will actually not work and not throw an error.
Is this a know bug or a feature? If it's a bug I'd like to contribute myself to fix it :)
Change History (1)
comment:1 by , 6 years ago
Component: | Uncategorized → Migrations |
---|---|
Resolution: | → worksforme |
Status: | new → closed |
Version: | 2.2 → master |
Migration works as expected in a described use case. When you leave
timezone.now
as a default value then it will be added to a new migration:and a new field in all existing rows will be filled by
timezone.now()
.Please use one of support channels, if you need further support.