[Answer]-Multiple linked Django forms, one submit

1👍

Tricky to tell without more information, but it looks like you’re not saving your rolesFormset in the view. You need to call rolesFormset.save() alongside your form.save() call. Additionally, I suppose you want to attach the roles to the created app? Something like this in your view should work:

if request.method == 'POST':
    form = AppForm(request.POST)
    rolesFormset = RoleForm(request.POST)
    if form.is_valid() and rolesFormset.is_valid():
        app = form.save()
        roles = rolesFormset.save()
        for role in roles:
            app.roles.add(role)
        return redirect(reverse('index'))

Update: Presuming the models.py is out-of-date, and Role does in fact have a foreignKey to User, the problem will be that you’re setting a prefix here:

rolesFormSet = rolesInlineFormSet(request.POST, instance=app, prefix='roles')

but not in your buildRolesFormset function. In that function, do:

formset = RoleFormSet(prefix='roles')
👤Greg

Leave a comment