【发布时间】:2013-03-08 10:31:01
【问题描述】:
我的模型中有一个DateTime 字段,我正在寻找一种简单的方法让它在表单中看起来不错。类似于SelectDateWidget。
我一直在研究很多类似的问题,让管理员 datepicker 或 jquery 之类的东西工作似乎真的很棘手。 (这是我第一次使用Django,之前从未使用过jquery)。
所以,我使用 ChoiceField 代替 this 示例,但我也无法让它工作。我得到错误名称 'self' is not defined。我不能在这里使用self 吗?还是有更好的简单方法来做到这一点?我不需要花哨的日期选择器,只需要让用户轻松输入的东西。
class ProjectForm(ModelForm):
startdate = forms.DateField()
starthour = forms.ChoiceField(choices=((6,"6am"),(7,"7am"),(8,"8am"),(9,"9am"), ...))
startminute = forms.ChoiceField(choices=((0,":00"),(15,":15"),(30,":30"),(45,":45")))
class Meta:
model = Project
def clean(self):
starttime = time(int(self.cleaned_data.get('starthour')),
int(self.cleaned_data.get('startminute')))
return self.cleaned_data
try:
self.instance.start_time = datetime.datetime.combine(
self.cleaned_data.get("startdate"), starttime)
except TypeError:
raise forms.ValidationError("")
【问题讨论】:
-
Youy
try块应该在“清洁”功能下。