forms.py如何访问modelForms领域,这是因为模型通过
class BandForm(forms.ModelForm):
time_from = forms.ChoiceField(choices=[(8, "08:00"),(9, "09:00"),(10, "10.00"),(11, "11:00"),
(12, "12:00"),(13, "13:00"),(14, "14:00"),(15, "15:00"),
(16, "16:00"),(17, "17:00"),(18, "18:00"),(19, "19:00"),
(20, "20:00"),(21, "21:00"),(22, "22:00"),(23, "23:00")])
time_to = forms.ChoiceField(choices=[(8, "08:00"),(9, "09:00"),(10, "10:00"),(11, "11:00"),
(12, "12:00"),(13, "13:00"),(14, "14:00"),(15, "15:00"),
(16, "16:00"),(17, "17:00"),(18, "18:00"),(19, "19:00"),
(20, "20:00"),(21, "21:00"),(22, "22:00"),(23, "23:00")])
class Meta:
model = Entry1
exclude = ('created','date')
部分views.py
if request.method == "POST":
form = BandForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
if year and month and day:
form.date = datetime.date(int(year),int(month),int(day))
form.save()
可以说我想要做一些形式验证.py for my class Bandform
def clean_fieldname(self): ....
,
通常你会得到像这样的参数 - self.cleaned_data.get('field_name')
。所以,如果我尝试获取self.cleaned_data.get('time_from') - 所有东西都很棒。但是,如果我尝试获取self.cleaned_data.get('日期'),我得到None返回。
为什么?
回答
Django Modelform (with excluded field) 这是从我提供的帖子有点不清楚,你必须在forms.py访问实例为self.instance.fieldname但不self.cleaned_data.get(唯一'字段名')。
因为它不存在? – 2011-04-20 17:09:21
在models.py我有一个类Entry1与字段日期= models.DateField()。如果我把它auto_now_add = True它自动插入日期,一切正常。如果我在保存表单之前在视图中插入日期,它怎么不存在? – Viktor 2011-04-20 17:17:58