Say I have two basic classes, one with a ManyToMany relationship to the other:
class Foo(models.Model):
abc = models.TextField()
test = models.ManyToManyField(Bar)
class Bar(models.Model):
zyx = models.TextField()
And then I create a Django form for Foo:
class FooForm(forms.ModelForm):
class Meta:
model = Foo
fields = ["abc", "test"]
This will create a form with a TextArea for me to input the value of the new Foo
's abc
variable, as well as a listbox to link it to one or more Bar
objects. What I would like to do for this particular form, is instead of the listbox for the Bar
objects, be able to create one (and only one) new Bar
at the same time (i.e., display a second TextArea for the value of Bar
's zyx
) and then link that new Bar
to the new Foo
. The ManyToMany relationship comes in later on in editing, but at creation, I want exactly one Bar
tied to the one new Foo
.
Is it possible to do this all at once by POSTing the form, or am I going to have to use some javascript and AJAX to actually create the Bar
first, get its new ID, and link the new Foo
to that?