Opened 8 years ago

Closed 6 years ago

Last modified 5 years ago

#10970 closed (fixed)

auto_now_add for DateField evaluetes to datetime.datetime instance

Reported by: Ivan Gromov Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords: DateField, auto_now_add
Cc: summer.is.gone@…, cyberj@…, jjlorenzo@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I've set auto_now_add to DateField, but it evaluates to datetime.datetime instance rather than datetime.date.
Here some example:

code in models.py

class Order(models.Model):
    created = models.DateField(auto_now_add=True)

and manage.py shell output:

In [1]: from app.models import Order
In [3]: o = Order()
In [4]: o.save()
In [5]: o.created
Out[5]: datetime.datetime(2009, 5, 1, 2, 53, 32, 280919)

I'm using django-1.0.2-final

Attachments (3)

auto_now_patch.diff (2.1 KB) - added by Ivan Gromov 7 years ago.
auto_now_patch_1.diff (2.0 KB) - added by Cyberj 7 years ago.
Patch for rev 12847
10970.diff (2.6 KB) - added by Ramiro Morales 6 years ago.
Patch updated to r14002 (test fixed to actually fail before the fix and updated to unittest)

Download all attachments as: .zip

Change History (15)

comment:1 Changed 8 years ago by Ivan Gromov

Checked out to revision #10650, bug still there

comment:2 Changed 8 years ago by Alex Gaynor

Patch needs improvement: set

This patch doesn't work as DateTimeField relies on inhering this method.

comment:3 Changed 8 years ago by Ramiro Morales

Has patch: set
Needs tests: set

comment:4 Changed 7 years ago by Tyson Clugg

Surely datetime.date.today() is more concise than datetime.datetime.now().date() - is there a good reason why the latter was selected?

Changed 7 years ago by Ivan Gromov

Attachment: auto_now_patch.diff added

comment:5 Changed 7 years ago by Ivan Gromov

Nope, there wasn't reason to use datetime.now().date() instead datetime.date.today(). I've updated patch, thank you.

comment:6 Changed 7 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

Changed 7 years ago by Cyberj

Attachment: auto_now_patch_1.diff added

Patch for rev 12847

comment:7 Changed 7 years ago by Cyberj

Cc: cyberj@… added
Needs tests: unset
Patch needs improvement: unset

This bug remains an issue for applications like http://code.google.com/p/django-reversion/ because it takes a serialized datetime object for datefield with auto_now_add.

I've updated patch for r12846 and tests are OK

comment:8 Changed 6 years ago by Ramiro Morales

See #14037 for a manifestation of this bug when using Django's own serialization machinery)

comment:9 Changed 6 years ago by José Jorge Lorenzo Vila

Cc: jjlorenzo@… added

Changed 6 years ago by Ramiro Morales

Attachment: 10970.diff added

Patch updated to r14002 (test fixed to actually fail before the fix and updated to unittest)

comment:10 Changed 6 years ago by Eric Holscher

milestone: 1.3
Triage Stage: AcceptedReady for checkin

comment:11 Changed 6 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

(In [14029]) Fixed #10970 -- Initialize DateFields with datetime.date objects, not datetime.datetime. Thanks, summerisgone, Cyberj and Ramiro Morales.

comment:12 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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