Here is how my models are setup:
class ModelA(models.Model):
user = models.ForeignKey(User)
modelB = models.ForeignKey('ModelB', related_name='modelB')
# more fields here
class ModelB(models.Model):
user = models.ForeignKey(User)
# more fields here
The user field in both classes are just so that both of these can be queried by the same user later on.
I have a ModelForm of ModelB and I would like to have the users be able to fill out a ModelB ModelForm with a dynamic number of ModelA instances. I would like ModelB’s form to start out with 1 instance of ModelA to fill out and then the user should be able to add more ModelA’s to ModelB’s form.
I’m stuck on this as a personal project and would really like to move forward. Thanks for any help!
Edit 1: I know that I will most likely need to do some work by overloading ModelForms def __init__, but that is about as far as I have gotten. Also I am using ClassBasedGeneric Views (CreateView to get the add working and an EditView later on), but I am willing to move away from that if needed.
You require django inline formset
In view:
In template lets suppose you are rendering form_set in a
divwhose id isone_to_many:Now the
jquerypart to add field dynamically:You can test this on jsfiddle. Note this will only work if you render your form
as_table