ModelForm如果您已经在
fields属性中提到了字段,则无需在中重新定义字段。因此您的表格应如下所示-
class SelectCourseYear(forms.ModelForm): class meta: model = Student fields = ['course', 'year'] # removing user. we'll handle that in view
def step3(request): user = request.user if request.method == 'POST': form = SelectCourseYear(request.POST) if form.is_valid(): student = form.save(commit=False) # commit=False tells Django that "Don't send this to database yet. # I have more things I want to do with it." student.user = request.user # Set the user object here student.save() # Now you can send it to DB return render_to_response("registration/complete.html", RequestContext(request)) else: form = SelectCourseYear() return render(request, 'registration/step3.html',)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)