更新时间:2023-12-06 16:58:58
有时这是最简单的解决方案。只需复制request.POST数据并修改TOTAL-FORMS。
例如..
if request.method =='POST':
PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
如果在request.POST中添加:P $ = request.POST.copy()
cp ['prim-TOTAL_FORMS'] = int(cp ['prim-TOTAL_FORMS'])+ 1
prims = PrimaryFunctionFormSet(cp,prefix ='prim')
然后只要正常地吐出表单。保存数据,添加内联编辑器。
This post relates to this: Add row to inlines dynamically in django admin
Is there a way to achive adding inline formsets WITHOUT using javascript? Obviously, there would be a page-refresh involved.
So, if the form had a button called 'add'...
I figured I could do it like this:
if request.method=='POST':
if 'add' in request.POST:
PrimaryFunctionFormSet = inlineformset_factory(Position,Function,extra=1)
prims = PrimaryFunctionFormSet(request.POST)
Which I thought would add 1 each time, then populate the form with the post data. However, it seems that the extra=1 does not add 1 to the post data.
Got it.
Sometimes it's the simplest solution. Just make a copy of the request.POST data and modify the TOTAL-FORMS.
for example..
if request.method=='POST':
PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
if 'add' in request.POST:
cp = request.POST.copy()
cp['prim-TOTAL_FORMS'] = int(cp['prim-TOTAL_FORMS'])+ 1
prims = PrimaryFunctionFormSet(cp,prefix='prim')
Then just spit the form out as normal. Keeps your data, adds an inline editor.