Code

Opened 5 years ago

Closed 4 years ago

Last modified 3 years ago

#10970 closed (fixed)

auto_now_add for DateField evaluetes to datetime.datetime instance

Reported by: summerisgone 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 summerisgone 5 years ago.
auto_now_patch_1.diff (2.0 KB) - added by Cyberj 4 years ago.
Patch for rev 12847
10970.diff (2.6 KB) - added by ramiro 4 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 5 years ago by summerisgone

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

Checked out to revision #10650, bug still there

comment:2 Changed 5 years ago by Alex

  • Patch needs improvement set

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

comment:3 Changed 5 years ago by ramiro

  • Has patch set
  • Needs tests set

comment:4 Changed 5 years ago by tyson

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

Changed 5 years ago by summerisgone

comment:5 Changed 5 years ago by summerisgone

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

comment:6 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

Changed 4 years ago by Cyberj

Patch for rev 12847

comment:7 Changed 4 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 4 years ago by ramiro

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

comment:9 Changed 4 years ago by jjlorenzo

  • Cc jjlorenzo@… added

Changed 4 years ago by ramiro

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

comment:10 Changed 4 years ago by ericholscher

  • milestone set to 1.3
  • Triage Stage changed from Accepted to Ready for checkin

comment:11 Changed 4 years ago by jezdez

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

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

comment:12 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.