我正在尝试为此问题找到某种规范参考,其中我使用以下类型模型:
class MyPreferencesForm(forms.Form):
MyGenderPref = forms.MultipleChoiceField(choices=GENDER_CHOICES, widget=forms.CheckboxSelectMultiple())
和视图
def GoPreferences(request):
if request.method == "POST":
f = MyPreferencesForm(request.POST)
if f.is_valid():
model_instance = f.save(commit=False)
model_instance.save()
f.save_m2m()
return redirect('/')
else:
f = MyPreferencesForm()
return render(request, "mypreferences.html", {'form': f})
但是,我尝试遵循我认为明智的做法,并包括多对多。但我仍然无法完成这项工作。我的错误如下:
Exception Type: AttributeError
Exception Value:
'MyPreferencesForm' object has no attribute 'save'
错误来自包含的行model_instance = f.save(commit=False)