[Answered ]-Django Form with dependent ForeignKey forms on one Page

1👍

Try to Change this part

        if product_form.is_valid() and product_clothing_form.is_valid():
            new_product = product_form.save(commit=False)
            product_clothing_form.save(commit=False)
            new_product.store = store
            new_product.owner = request.user.profile
            product = product_form.save()
            product = product
            product_clothing_form.product = product
            product_clothing_form.save()
            print(request.user.first_name)
            return redirect('/')

to

        if product_form.is_valid() and product_clothing_form.is_valid():
            new_product = product_form.save(commit=False)
            product_clothing_form = product_clothing_form.save(commit=False)
            new_product.store = store
            new_product.owner = request.user.profile
            new_product.save() #this is correct your mistake was here
            print(new_product)
            product_clothing_form.product = new_product
            product_clothing_form.save()
            print(request.user.first_name)
            return redirect('/')

Leave a comment