Skip to content
Advertisement

form for simultaneously editing two django models with foreign key relationship

I am trying to find a simple way to create a form that allows the editing of two models with foreign key relationship simultaneously.

After some research, it seems that Inline formsets come very close to what I want to do.

The django documentation offers this example:

JavaScript

And then,

JavaScript

Let’s suppose Author has a second field, city. Can I use the fields argument to add a city to the form?

If inline formsets are not the way to go, is there another way that generates this joint form?


After some more research, I found django model Form. Include fields from related models from 2009 which hints that inline form sets might not be the way to go.

I would be very much interested if there’s a default solution with a different factory.

Advertisement

Answer

Well, this is a bit different from the linked post because there the relationship is a OneToOne and not a ForeignKey.

There is no django factory (at least that I know of) to do what you want automatically. You can try the following instead:

  • Create a ModelForm for the depended table (Book in this case):

    JavaScript
  • Create an inline_formset for the depended table:

    JavaScript
  • Use the formset in your view:

    JavaScript

    OR in a class based view: django class-based views with inline model-form or formset

  • Finally in the template (this part needs a bit of fumbling to get it right, but this is a general idea):

    JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement