简单来讲:
如果你想传入前端的页面中附带值,那么在实例化forms中:
form = SecondModelForm(data=request.POST, instance=permission_obj)
return render(request, 'rbac/change.html', {'form': form})
该参数,会把所有的值都会传入到form中
如果你想传入前端的页面中附带某一个值,那么在实例化forms中:
form = SecondModelForm(initial={"字段名": 值})
return render(request, 'rbac/change.html', {'form': form})
如果在保存数据库中,需要默认传入一个值,那么form.save()之前做:
second_menu_obj = models.Permission.objects.filter(id=second_menu_id).first()
if not second_menu_obj:
return HttpResponse("二级菜单不存在")
form.instance.pid = second_menu_obj
form.save()
标签:obj,21,form,menu,initial,forms,instance,second
From: https://www.cnblogs.com/mimiICC/p/16646661.html