我正在使用基于类的通用视图,有人可以建议我如何设置初始值来更新表单吗?
我尝试使用 get_initial() 方法,但没有成功。以下是我尝试过的代码
class IncidentUpdateView(UpdateView):
form_class = IncidentForm
form_class.initial = {"badge_number": '88888'}
model = Incident
template_name = 'hse/incident/incident_update.html'
def get_initial(self, form_class):
initials = {
"badge_number": '88888'
}
form = form_class(initial=initials)
return form
def get_success_url(self):
return reverse_lazy('hse-incident', args=[self.object.id])