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).
--
Ticket URL: <https://code.djangoproject.com/ticket/24497>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted
Comment:
I guess
[https://github.com/django/django/blob/53d11c6821b7b347e0eeefb96168cc1861715a6b/django/forms/forms.py#L618-L622
this is the offending code].
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:1>
Comment (by 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?
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:2>
Comment (by bmispelon):
Documentation issue tracked in #24520.
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:3>
* cc: jiguromusha7@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:4>
Comment (by riklaunim):
Replying to [comment:2 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?
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:5>
Comment (by wdmgsm):
It was suggested by bmispelon in this
[https://github.com/django/django/pull/4373 pull request] to add
{{{supports_microseconds}}} to the base {{{Widget}}} class so that all
derived widgets inherit it.
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:6>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/4467 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:7>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:8>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:9>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"4dcc6493418c78db07761180bf6265f5b2bbccbf" 4dcc649]:
{{{
#!CommitTicketReference repository=""
revision="4dcc6493418c78db07761180bf6265f5b2bbccbf"
Fixed #24497 -- Added Widget.supports_microseconds attribute
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24497#comment:10>