Django Multiple Forms from related Models
I am pretty new to Django and have come stuck on how to approach an issue I have with collecting Data via multiple forms.
I have the following models, the first being where I store data of an asset:
class Model_AssetData(models.Model):
UU_Project_Number = models.ForeignKey(Model_Project, null=True, on_delete=models.CASCADE)
Site_Code = models.CharField(blank=True, null=True,max_length=5)
Facility = models.ForeignKey(Model_Facility, null=True, on_delete=models.CASCADE)
Facility_Number = models.IntegerField(blank= False, null= True,)
Process = models.ForeignKey(Model_Process, null=True, on_delete=models.CASCADE)
Process_Number = models.IntegerField(blank= False, null= True)
Arrangement = models.ForeignKey(Model_Arrangement, null=True, on_delete=models.CASCADE )
Arrangement_Number = models.IntegerField(blank= False, null= True)
Asset_Group = models.ForeignKey(Model_AssetLocation, null=True, on_delete=models.CASCADE )
Asset_Group_Number = models.IntegerField(blank= False, null= True)
Asset_Number = models.CharField(blank=False, null=False,max_length=50)
Equipment_Group_Identifiier = models.ForeignKey(Model_EGI, null = True, on_delete=models.CASCADE)
Engineering_Design_Identifier = models.ForeignKey(Model_EDI, null = True, on_delete=models.CASCADE)
ITR = models.ForeignKey(Model_ITR, null = True, on_delete=models.SET_NULL)
def __str__(self):
return self.Asset_Number
def save(self, *args, **kwargs):
# Only generate key on creating if it was not provided
if not self.id:
# Use f-string to concatenate the asset code into the Database
self.Asset_Number = f'{self.UU_Project_Number}-{self.Site_Code}-{self.Facility}
{self.Facility_Number}-{self.Process}{self.Process_Number}-{self.Arrangement}
{self.Arrangement_Number}-{self.Asset_Group}{self.Asset_Group_Number}'
super().save(*args, **kwargs)
Then I am trying to collect basically some answers to questions to ensure certain things have been checked when the asset was installed: This model is a reference to what questions need to be asked for a particular asset:
class Model_ITR(models.Model):
ITR_Reference = models.CharField( blank=True, null=True,max_length=50)
ITR_Description = models.CharField( blank=True, null=True,max_length=50)
def __str__(self):
return self.ITR_Reference
Then the following models hold the questions, link the questions to the ITR and then store the data respectively:
class Model_ITR_Questions(models.Model):
ITR_Question = models.CharField( blank=True, null=True,max_length=50)
def __str__(self):
return self.ITR_Question
class Model_ITR_Bridge(models.Model):
ITR = models.ForeignKey(Model_ITR, null=True, on_delete=models.SET_NULL)
Question = models.ForeignKey(Model_ITR_Questions, null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.ITR
class Model_ITR_Data(models.Model):
Asset_Number = models.ForeignKey(Model_AssetData, null=True, on_delete=models.SET_NULL)
Question = models.ForeignKey(Model_ITR_Bridge, null=True, on_delete=models.SET_NULL)
Data = models.CharField( blank=True, null=True,max_length=500)
def __str__(self):
return self.Data
Basically I need to render in a template the applicable list of questions and a form to input the response ( more than likely a charfield) into the Model.
My first thoughts were an inline formset... but I am struggling a bit with how I'd get the questions into the template, I've thought about maybe trying to set the label of the form for each instance.
Or would it be better to just pass the query set into the template and set up for loops for the questions and data fields.. but again I am a bit unsure how to handle the potential multiple submits from the template to the view.
Does anyone have any ideas or could provide some help into the best approach to take?