django的modelform关于save()方法的使用说明

参考 django 官方文档 https://docs.djangoproject.com/zh-hans/2.0/topics/forms/modelforms/

首先,创建的form类,会有个meta类,

meta类里的要定义一个model,即是form表单连接的model,然后fields里是你所需要的model的字段,

就像这样

    class Meta:
        model=Model_Supply
        fields = ('Supply_user','ModelTitle','Introduction','ModelContext','ModelUses','Algorithm','Qualifications','Price','Photo','Appraisal','SellingMethod','TradeClass_S','TargetClass_S') 
 

此时,在后端使用save方法时,若数据通过后台获取,而不是前端获取,则可以在view中使用如下方法:

 form = Model_Supply_Form(request.POST,request.FILES)

 if form.is_valid():          
       uid = id
       new_model = form.save(commit=False)    
       new_model.Supply_user = uid
       new_model.save()
此时,就可以保存你所需要的数据到数据库中了

猜你喜欢

转载自blog.csdn.net/qq_21570029/article/details/79728458