我有以下形式:Django 1.5 SplitDateTimeWidget發送到form.clean()的數據是什麼?
class TripForm(IntranetForm):
def __init__(self, *args, **kwargs):
super(TripForm, self).__init__(*args, **kwargs)
self.helper.layout = Layout(
Field('reason', css_class='input-xlarge'),
Field('departure_date'),
Field('return_date'),
Field(
'date_and_time_of_first_appointment',
),
Field(
'date_and_time_final_appointment_finishes',
),
Field(
'departure_location',
template='travel/related_departure.html',
),
Field(
'destination',
template='travel/related_destination.html',
),
Field('mode_of_transport', css_class='input-xlarge'),
Field('seating_preference', css_class='input-xlarge'),
Field('special_requests', css_class='input-xlarge'),
FormActions(
Submit(
'save_changes',
'Save changes',
css_class = "btn-primary",
),
Button(
'cancel',
'Cancel',
onclick = 'history.go(-1);'
),
),
)
def clean(self):
cleaned_data = super(TripForm, self).clean()
departure_date = cleaned_data.get("departure_date")
return_date = cleaned_data.get("return_date")
a1 = cleaned_data.get("date_and_time_of_first_appointment")
af = cleaned_data.get("date_and_time_final_appointment_finishes")
if departure_date < datetime.date.today():
msg = u"Must be a date in the future."
self._errors["departure_date"] = self.error_class([msg])
del cleaned_data["departure_date"]
if a1.date() < departure_date:
msg = u"Must be after the departure date."
self._errors["date_and_time_of_first_appointment"] = self.error_class([msg])
del cleaned_data["date_and_time_of_first_appointment"]
if return_date < departure_date:
msg = u"Must be after the departure date."
self._errors["return_date"] = self.error_class([msg])
del cleaned_data["return_date"]
if af < a1 or af.date() > return_date:
msg = u"Must be after the first appointment and before the return date."
self._errors["date_and_time_final_appointment_finishes"] = self.error_class([msg])
del cleaned_data["date_and_time_final_appointment_finishes"]
return cleaned_data
class Meta:
model = Trip
fields = (
'reason',
'departure_date',
'return_date',
'date_and_time_of_first_appointment',
'date_and_time_final_appointment_finishes',
'departure_location',
'destination',
'mode_of_transport',
'seating_preference',
'special_requests',
)
widgets = {
'date_and_time_of_first_appointment': SplitDateTimeWidget(),
'date_and_time_final_appointment_finishes': SplitDateTimeWidget(),
}
測試類似於下面所有的一個正常工作的時候我不使用SplitDateTimeWidget
。
def test_trip_form_with_good_data(self):
form_data = {
'reason': 'HE',
'departure_date': timezone.datetime.today(),
'return_date': timezone.datetime.today(),
'date_and_time_of_first_appointment': timezone.now(),
'date_and_time_final_appointment_finishes': timezone.now(),
'departure_location': 1,
'destination': 1,
'mode_of_transport': 'TR',
'seating_preference': 'Near the front',
'special_requests': 'Make it nice',
}
form = TripForm(data=form_data)
self.assertTrue(form.is_valid())
但是,當使用的SplitDateTimeWidget
是爲DateTime
場測試不再運行。他們把錯誤,而不是失敗,具體如下:
AttributeError: 'NoneType' object has no attribute 'date'
每當a1
變量在我重寫的形式clean
方法來訪問。
我已經看過相關的源代碼,並且看不到爲什麼使用SelectDateTimeWidget
會在命令行或測試中需要不同的輸入,但顯然它確實如此。我的測試應該如何以clean
方法能夠訪問的方式提供數據?
編輯: 以我的線索,從所提供的HTML我也嘗試讓不同的日期和時間從領域,如date_and_time_of_first_appointment_0
和date_and_time_of_first_appointment_1
和他們在clean
方法,但無濟於事比較之前組合。