Django has formsets, where multiple forms can be used in one big form. So let's say one can add in a e.g. library formset mulitple books (providing the author and title) using repetitions of the same book form.
How to achieve the same functionality with Angular.js and Django Rest Framework? I'm new to Angular.js and Django Rest Framework and need some guidance how to be able to dynamically add more forms(e.g. for a book) for a given model in one big form (e.g. my library) and save them in Django Backend.
You can achieve this in 2 steps:
On Frontend
Create a
<form>
on your page that will structure the data entered by the user as you need. Inside that<form>
element, you'll need to use thengForm
for multiple forms' validation to behave correctly (here is a nice explanation of howngForm
works). A hypothetical code snippet would look like:In your controller, you can initialize the list of books to add as
vm.newBooksToAdd = [];
and whenever you want to add a new form to your list of forms for new books, justvm.newBooksToAdd.push({})
an empty object. Thus, you will send to the backend an array of objects representing books you want to create.On Backend
Now here you'll need to overwrite the
.create()
method of your view to allow creating many instances at once, because by default it expects a single object. Your view might look like this:Note: If you would like to allow both a single instance creation and creation in bulk, you'll need to adjust your
.create()
method to check for the data type ofrequest.data
.Note 2: There is a
django-rest-framework-bulk
library that achieves what you want on the backend, but I didn't try it, so cannot say anything bad or good about it.Good luck!