Opened 9 years ago

Closed 9 years ago

#24497 closed Bug (fixed)

Truncation of microseconds in DateTimeField leads to lookup trouble

Reported by: Piotr Maliński Owned by: nobody
Component: Forms Version: 1.7
Severity: Normal Keywords: microseconds
Cc: jiguromusha7@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've stumbled upon an yet another inconsistency in Django forms with microseconds handling.

I have a model with DateTime field set with timezone.now(). In a form this date is passed as hidden so that before save it can be verified that the content didn't changed during moderation. The problem is that timezone.now() sets microseconds, while datetime returned in form_valid is without microseconds and thus can't be used for direct lookup on that model field (and that what hurts).

Change History (10)

comment:1 by Tim Graham, 9 years ago

Summary: Form datetime inconsistent handlingTruncation of microseconds in DateTimeField leads to lookup trouble
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Claude Paroz, 9 years ago

It might be a use case for the supports_microseconds widget property (which needs documentation, by the way).

Could you try to subclass HiddenInput, set supports_microseconds class attribute to True for your subclass, and then use that widget in your form?

comment:3 by Baptiste Mispelon, 9 years ago

Documentation issue tracked in #24520.

comment:4 by Panos Katseas, 9 years ago

Cc: jiguromusha7@… added

in reply to:  2 comment:5 by Piotr Maliński, 9 years ago

Replying to claudep:

It might be a use case for the supports_microseconds widget property (which needs documentation, by the way).

Could you try to subclass HiddenInput, set supports_microseconds class attribute to True for your subclass, and then use that widget in your form?

Depends what should be the default behavior for a ModelForm? Field in a model should be tailored to a matching form field and it validation rules. If Django must permanently modify DatetTimes for display and alike purposes then it should be explicit and consistent with models. If form field has supports_microseconds then maybe DateTimeField in a model should also have such attribute so that setting timezone.now()/datetime.now() etc. won't save with microseconds in the first place?

comment:6 by wdmgsm, 9 years ago

It was suggested by bmispelon in this pull request to add supports_microseconds to the base Widget class so that all derived widgets inherit it.

comment:7 by Tim Graham, 9 years ago

Has patch: set

comment:8 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:9 by Tim Graham, 9 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

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

Resolution: fixed
Status: newclosed

In 4dcc649:

Fixed #24497 -- Added Widget.supports_microseconds attribute

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