Django,保存ModelForm

Django,保存ModelForm,第1张

Django,保存ModelForm

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',)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5650186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存