Opened 9 years ago

Closed 8 years ago

#25005 closed Bug (fixed)

DateField, DateTimeField defaults needed even when auto_now=True

Reported by: monobotsoft Owned by: Iacopo Spalletti
Component: Migrations Version: dev
Severity: Normal Keywords: DateField, DateTimeField, TimeField
Cc: me@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you create a new DateField on a model even when it has auto_now=True.

./manage.py makemigrations asks for a default value

This is a low priority bug since its so easylly solved using default=timezone.now

Attachments (1)

ticket_25005.patch (2.9 KB ) - added by Andriy Sokolovskiy 9 years ago.
Tests to reproduce the issue

Download all attachments as: .zip

Change History (14)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

by Andriy Sokolovskiy, 9 years ago

Attachment: ticket_25005.patch added

Tests to reproduce the issue

comment:2 by Andriy Sokolovskiy, 9 years ago

Cc: me@… added
Owner: changed from nobody to Andriy Sokolovskiy
Status: newassigned

comment:3 by Andriy Sokolovskiy, 9 years ago

Changing autodetector is not a problem.
But...
When we have some model, then adding DateField or DateTimeField with auto_now/auto_now_add, and disable promting for default, it throws integrity errors for null. So seems it not works on existing objects. Is that normal behavior or it is a bug?

comment:4 by Andriy Sokolovskiy, 9 years ago

So, seems there changes in effective_default are needed, so I'm going this way

in reply to:  3 comment:6 by Markus Holtermann, 9 years ago

Replying to coldmind:

When we have some model, then adding DateField or DateTimeField with auto_now/auto_now_add, and disable promting for default, it throws integrity errors for null. So seems it not works on existing objects. Is that normal behavior or it is a bug?

That's expected behavior. auto_now and auto_now_add are purely handled in Python (default too, but on a different level) and thus don't influence the default value for the database.

As far as I can tell, combining auto_now, auto_now_add and default is not allowed, since those three options are supposed to be mutual exclusive: https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.DateField

comment:7 by Andriy Sokolovskiy, 9 years ago

Keywords: TimeField added
Version: 1.8master

Opened new PR - https://github.com/django/django/pull/4902
This will not backported to 1.8

comment:8 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:9 by Iacopo Spalletti, 9 years ago

Owner: changed from Andriy Sokolovskiy to Iacopo Spalletti

Assigning to myself to bring PR in mergeable state

comment:10 by Tim Graham, 9 years ago

Patch needs improvement: unset

comment:11 by Tim Graham, 9 years ago

Patch needs improvement: set

The new test on the updated PR from yakky isn't passing on Oracle.

Last edited 9 years ago by Tim Graham (previous) (diff)

comment:12 by Tim Graham, 9 years ago

Patch needs improvement: unset

comment:13 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 49c57f85:

Fixed #25005 -- Made date and time fields with auto_now/auto_now_add use effective default.

Thanks to Andriy Sokolovskiy for initial patch.

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