Opened 10 years ago
Closed 10 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 , 10 years ago
Summary: | Form datetime inconsistent handling → Truncation of microseconds in DateTimeField leads to lookup trouble |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
follow-up: 5 comment:2 by , 10 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:4 by , 10 years ago
Cc: | added |
---|
comment:5 by , 10 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
, setsupports_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 , 10 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:8 by , 10 years ago
Patch needs improvement: | set |
---|
comment:9 by , 10 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
I guess this is the offending code.