I have two models that look something like
class Address(models.Model):
line1 = models.CharField(max_length=128, help_text="Address Line 1")
city = models.CharField(max_length=128)
state = USStateField()
zipcode = USZipCodeField()
class Company(models.Model):
name = models.CharField(max_length=100)
address = models.ForeignKey('Address', on_delete=models.PROTECT)
And I would like to generate a form that looks something like below, although I have no idea how to save the address from within a view without hard coding each individual fields from within a view.
<input type=text id="name">Name</input>
<input type=text id="line1">Address Line1</input>
<input type=text id="city">City</input>
<input type=text id="state">State</input>
<input type=text id="zipcode">Zipcode</input>
The closest thing I have come up with is something like
class CustomForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomForm, self).__init__(*args, **kwargs)
address_fields = forms.fields_for_model(Address, exclude=())
self.fields.update(address_fields)
class Meta:
model = Company
exclude = ['address']
Is there a known/best-practice way of accomplishing this?