我被困住了,我一直在试图了解如何覆盖save方法,而我却被困在如何访问需要保存的字段上。我已经用尽了堆栈交换和文档,但是无法正常工作。
Class ProjectModelForm(forms.ModelForm): “” 用于创建和编辑项目的表格。认证用户 需要登录。 “” 标题= Forms.CharField(widget = forms.Textarea) 摘要= Forms.CharField(widget = forms.Textarea) others = form.CharField(widget = forms.Textarea,required = False)
class Meta:
model = Project
fields = (
'area',
'title',
'summary',
'others',
'staff_trials',
'launch',
)
# widgets = {
# 'launch': DateInput(),
# 'staff_trials': DateInput(),
# }
def __init__(self, *args, **kwargs):
super(ProjectModelForm, self).__init__(*args, **kwargs)
self.fields['title'].widget.attrs['cols'] = 100
self.fields['title'].widget.attrs['rows'] = 1
self.fields['summary'].widget.attrs['cols'] = 100
self.fields['summary'].widget.attrs['rows'] = 8
self.fields['others'].widget.attrs['cols'] = 16
self.fields['others'].widget.attrs['rows'] = 3
def clean(self):
cleaned_data = super().clean()
staff_trials = cleaned_data.get("staff_trials")
launch = cleaned_data.get("launch")
timeline_st = cleaned_data.get("timeline_st")
if launch < date.today():
raise forms.ValidationError(
"Date must be a future date."
)
if staff_trials:
if staff_trials < date.today():
raise forms.ValidationError(
"Date must be a future date."
)
if launch < staff_trials:
raise forms.ValidationError(
"Launch date cannot be before Staff Trials."
)
return cleaned_data
def save(self, commit=True):
instance = super(ProjectModelForm, self).save(commit=False)
timeline_st = self.clean_data['timeline_st']
timeline_st = True
if commit:
timeline_st.save()
return instance
如何发现访问字段所需的方法,看来它们都是不同的。