#22208 closed Uncategorized (invalid)
Weird SplitDateTime widget Behaviour regarding initial data between bound & unbound forms
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | 1.6 |
Severity: | Normal | Keywords: | SplitDateTimeWidget, ModelForms |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
I need to send initial date & time to a SplitDateTime widget in a model form
an example:
models.py
class Event(models.Model): event_name = models.CharField(max_length = 250) start_date = models.DateTimeField(db_index = True, ) end_date = models.DateTimeField(db_index = True)
forms.py
class add_event (forms.ModelForm): start_date = forms.SplitDateTimeField(input_time_formats=['%I:%M %p'], widget=forms.SplitDateTimeWidget(date_format='%Y-%m-%d', time_format='%I:%M %p')) end_date = forms.SplitDateTimeField(input_time_formats=['%I:%M %p'], widget=forms.SplitDateTimeWidget(date_format='%Y-%m-%d', time_format='%I:%M %p'))
views.py
import datetime as dt def myview(request): data={} data['event_name'] = 'Awesome Event' data['start_date'] = dt.datetime.now #Now i send the 'initial' data to form , I Tried the following: form = add_event(initial=data) #1st: Works fine,everything is populated as expected, But i get an unbound form , with no validation form = add_event(data) # 2nd: event_name field is populated right but the SplitDatetime widget is not, (come out as an Empty widget) form = add_event(data=data) #3rd: Same as previous , Event_name field is populated right but the SplitDatetime widget is not
However , removing the splitDatetimeWidget from the add_event fields definition , would make the 3rd solution working & everything is populated fine
But results of a combined text field with both the date & time.
Searched over the internet , tried every solution , none seem to work , although i know there is some way of doing it , or else , CBV updating an event would contains empty SplitDateTime value as well. but i can't find the solution or deeper diagnose the problem.
What is missing here?
Perhaps this can be added to documentation. (If it's not already there and i didn't see it)
Thank you so much !!
Best regards;
Change History (2)
comment:1 by , 11 years ago
comment:2 by , 10 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Hi Ramezashraf,
Next time, please use Django's support channels instead of creating a ticket. You will get much faster support for issues like these.
https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
This is not a bug. A couple points:
1) Running "form = add_event(data=data)" and "form = add_event(data)" is the same.
2) The "initial" and "data" arguments are different: initial should receive values to be used when first rendering the form, and the data is not validated. "data" argument is used with the data that came from a submitted form. When the form is submitted, the date and time are split like this:
data['start_date_0'] = '2014-04-15' data['start_date_1'] = '07:33 PM' form = add_event(data=data)
This does work as expected. But for your case, you do want to pass that dictionary to "initial".
If you have further questions, please use the link above to get help with Django.
I also tried adding time zone , sending the date in sting , in list .. none seem to work with parameter 'data'
Yet the same dictionary sent to the form with initial argument (instead of data) works like a charm
The only problem persist in SplitDatetime , other widgets/field are populated as expected
am i missing something here or is it a bug ?!