make multiple forms related to multiple models on one HTML

The case of my question is quite complicated. Now I am making with a product stock db and many customers will request us for multiple products.

#The simply model as below:
class Product:{*product_ID, stock_quantity}

class Request:{*request_ID, customer_details…}

class Requested_Product:{*requested_product_ID, request_ID, product_ID,requested_quantity}

Here I wanna make one single page (just one view) for applying a request, with below criterias,

1.) Customer could request many products, that is the reason why I made another model {Requested_Product}, for avoiding repeated records of saving [customer_details] if combining {Requested_Product} and {Request} in one model.

2.) The page is able to save below forms simultaneously:

  • one record for {Request}
  • one/more records for {Requested_Product}
    (depends on the client, there is a button to add row for providing more forms if the client needs more products in the request)

3.) Regarding to the requested quantity, the stock quantity for each corresponding product could reduce automatically as ordered.

Please provide me with some directions/ref-links for me to handle it, I done searching but still could not make it.

my current situation is just able to deal with one request on sample (two forms refering two models) on the same page, but I couldnt deal with the case with (one request form, one/multiple product forms).

#views:
def Request(request):
if request.method == ‘POST’:
request_form = RequestForm(request.POST)
request_product_form = RequestProductForm(request.POST)

    if request_product_form.is_valid() and request_form.is_valid():
        Request = request_form.save()
        Request_Product = request_product_form.save(False)

        Request_Product.request_ID= Request
        Request_Product.save()

        return redirect(reverse('catalog/xxx.html'))
else:
    request_form = RequestForm()
    request_product_form = RequestProductForm()

args = {}
args.update(csrf(request))
args['request_form'] = request_form
args['request_product_form'] = request_product_form

return render(request, 'catalog/Request.html', args)

The Django facility you’re looking for is called formsets.

Also see Model formsets and Inline formsets.

<opinion>
You may want to spend some time to learn and understand how formsets work. They are very useful, but work differently than “regular” forms. I have encountered a lot of people (including myself) who found them a bit confusing at first to work with. I’d recommend working on a separate project as a “minimal implementation” to become familiar with them before trying to work them into your main project.
</opinion>

1 Like

Thanks pretty much, I did it by ref ur tutorial links. :grinning: