You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building a custom Django Form and defined a django.forms.DateTimeField with a UnfoldAdminSplitDateTimeVerticalWidget as a widget:
expired_at = forms.DateTimeField(
label=_("Expiration date time"),
help_text=_("When the request expires"),
required=False,
widget=UnfoldAdminSplitDateTimeVerticalWidget(
date_attrs={"type": "date"}, time_attrs={"type": "time"},
)
)
My goal is to obtain a field with Date and Time input using the Unfold component (as we can have on the demo site on date field) and returning the datetime value to my form.
Problem
When submitting the form, the form the following error happened:
the date and time input are provided to the form in two different keys.
As a consequence, the Field method to_python has to deal with the list: ['2024-12-20', '10:58'] which it cannot handle.
I had to overwrite the to_python method like this to make it works:
class CustomDateTimeField(forms.DateTimeField):
"""Custom DateTime Field to handle date and time data in list value"""
def to_python(self, value):
"""Handle date and time put in a list which is not handled by the generic field"""
value = datetime.strptime(f"{value[0]}T{value[1]}", "%Y-%m-%dT%H:%M")
super().to_python(value)
I suggest it is a bug because I can see the Date and Time widget from Unfold (although I don't know which class is used exactly) in a form is working well because default creation forms seem to use it. So maybe I'm using it wrong here. However, if it is the case, maybe UnfoldAdminSplitDateTimeVerticalWidget should be more straightforward to use.
The text was updated successfully, but these errors were encountered:
Are you using custom or localized datetime format? Few weeks ago I fixed something similar by adding new options to settings.py into Formula demo project:
Thanks for helping, I'm using standard datetime format.
However I've found the issue: I was using UnfoldAdminSplitDateTimeVerticalWidget with forms.DateTimeField which is not supported.
It seems that Widget with MultiWidgets must be used with suitable Fields, in my case: forms.SplitDateTimeFieldinstead of forms.DateTimeField.
This code is working well:
expired_at = forms.SplitDateTimeField(
label=_("Expiration date time"),
help_text=_("When the request expires"),
required=False,
widget=UnfoldAdminSplitDateTimeVerticalWidget(
date_attrs={"type": "date"}, time_attrs={"type": "time"},
)
)
unfold version: 0.43.0
django version: 5.1.4
browser: Version 1.73.104 Chromium: 131.0.6778.204 (Build officiel) (arm64)
checked other issues: Yes
checked documentation: Yes
Issue description
Context
I'm building a custom Django Form and defined a
django.forms.DateTimeField
with aUnfoldAdminSplitDateTimeVerticalWidget
as a widget:My goal is to obtain a field with Date and Time input using the Unfold component (as we can have on the demo site on date field) and returning the datetime value to my form.
Problem
When submitting the form, the form the following error happened:
'list' object has no attribute 'strip'
Traceback:
/.../lib/python3.12/site-packages/django/forms/fields.py
When I look at the POST data in the debug mode I've found this:
the date and time input are provided to the form in two different keys.
As a consequence, the Field method to_python has to deal with the list: ['2024-12-20', '10:58'] which it cannot handle.
I had to overwrite the to_python method like this to make it works:
I suggest it is a bug because I can see the Date and Time widget from Unfold (although I don't know which class is used exactly) in a form is working well because default creation forms seem to use it. So maybe I'm using it wrong here. However, if it is the case, maybe
UnfoldAdminSplitDateTimeVerticalWidget
should be more straightforward to use.The text was updated successfully, but these errors were encountered: