[Answered ]-How can I implement adding multiple objects with Django form?

2đź‘Ť

âś…

One way is to use a Formset. Formsets you may have seen, for example, in Django admin for displaying multiple related objects as inlines.

Another way could involve AJAX, example solution:

  • “Added products” is a simple <ul> list with products added to order
  • “Search product” is a plain text field, where user enters product name or SKU
  • User input is sent via AJAX to the server, which returns a list of relevant products
  • These product suggestions are displayed to user
  • When user clicks on a product name, another AJAX request is made to associate given product with the order
  • After request completes, the “Added products” list is refreshed via AJAX, and newly added product appears there

This would require that you first create a temporary order to which you could later attach products via separate requests. Formsets don’t have this requirement, but personally I haven’t used them a lot, they look a bit cumbersome.

Leave a comment