我制作了一个ModelForm
,其中包含Student
模型的所有字段,并在表单中添加了一个额外的选择字段。但是,既然我已经传递了选择字段,我想知道如何将其传递给数据库,因为例如,我想将选择的名称作为数据库中对象的名称传递,我将如何传递呢?去做?这就是仅应用模型字段的情况下FormView的样子:
class StudentDelete(generic.FormView):
template_name = 'students/student_form_delete.html'
form_class = DeleteStudentForm
success_url = '/'
def form_valid(self, form):
form.save(commit=True)
return super().form_valid(form)
我需要怎么做才能将选择字段添加到此处的数据库(这是我想去的地方)
def form_valid(self, form):
#logic to add the field to the database
form.save(commit=True)
return super().form_valid(form)
以下是表格,仅供参考:
class DeleteStudentForm(forms.ModelForm):
choice = forms.ModelChoiceField(queryset=Student.objects.all(), required=True, help_text="Student Name")
class Meta:
model = Student
fields = '__all__'