Django design : mulitple forms in a single view, CBV or FBV? inline_formsets?

I have three models related as follows:

Location
Business has a ForeignKey relationship to Location
Contact has a ForeignKey relationship to Business

class Location(models.Model):
    latitude =.. 

class Business(models.Model):
    home_location = models.ForeignKey(Location)


class Contact(models.Model):
    business = models.ForeignKey(Business, on_delete=models.CASCADE)
    contact = models.CharField(max_length=16, null=True)

All three models have their own individual Form class : LocationForm, BusinessForm, ContactForm

I want to have a Business page that can create (or update) a Business -- its location and associated Contact(s) on the same page.

So far, I have tried using 3 forms with prefixes with a CBV but connecting the POST request of three forms in form_valid() doesn't work as the Django doesn't recognize the form as valid. But I am not even sure if that's the right way to approach this problem. So my questions are:

  1. What is the Django way to approach the problem: Do I create a template with these three forms and somehow tie them up in a single View? If yes, Would a Class Based View work?

  2. Can inline Formsets work in this three way situation?

I'm still very new to Django so would appreciate help from the community to help me understand Django conceptually.

This community is incredibly helpful! Thank you all!

Back to Top