[Django]-Dynamically populating fields in a formset

5👍

If you never intend to have a user edit the sequence field and plan on always calculating it’s value on the back end, you can exclude it from the form, rather than having it hidden.

Then you can use commit=False and handle the calculation of your sequence field as needed.

👤Harold

-2👍

newPOST = request.POST.copy()
i=1
for index in range(0, int(request.POST["routebase_set-TOTAL_FORMS"])-1):
    if request.POST["routebase_set-" + str(index) + "-base"]:
        newPOST["routebase_set-" + str(index) + "-sequence"] = i
        i += 1
    else:
        newPOST["routebase_set-" + str(index) + "-sequence"] = ""

Honestly, this seems to work better than any messing around with form validation. It’s not as hacky as I thought it be either…

Leave a comment