Django: adding inline form lines without javascript

This post applies to this: Add a line dynamically dynamically in django admin

Is there a way to achieve adding inline forms without using javascript? Obviously, the page refresh will be involved.

So, if the form had a button named 'add' ...

I decided that 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, and then fill out the form with message data. However, it seems that extra = 1 does not add 1 to the message.

+4
source share
1 answer

Got it.

Sometimes this is the easiest solution. Just make a copy of the request.POST data and change the TOTAL-FORMS.

eg..

 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 out the form as usual. Saves your data, adds a built-in editor.

+5
source

Source: https://habr.com/ru/post/1304178/


All Articles