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