2

Let's say I have a class Customer, with a nested class Address. Each customer has an address, and the customer table has a foreign key, address_id, that points to the address table. A customer can only have one address.

Is there a good way in Django to have an AddressForm nested inside CustomerForm, so that I can just send the CustomerForm to the template, and the framework will automatically display nested address fields, collect data for both classes, and save both on the back-end?

I tried reading the docs for this, and googling it, but most solutions talk about using Formsets and InlineFormsets, which seem to mostly be about having multiple inline instances, but in this case, there is only one nested class.

The way I am doing it now is to basically have the two separate ModelForm classes, send them separately to the same template, and then save them separately, which should work, but I wonder if there is a better, more elegant way.

Thanks in advance.

0 Answers0