Django: How do I create a form of model A for each instance of model B

I am trying to creat an app, where the user fills out forms (model B). The forms are based on variables (model A), which are defined by the admin. The form should save the input in model B (i.e. input values) and therefore show the name/label of model A (i.e. the variable name) for each instance of A and the corresponding fields of B (the input value).

I am stuck with showing each inptut form seperately. Technically this works, but creates a terrible user experience. How do I render all input forms for each variable on one page?

I would appreciate any help greatly!

Model A (variables):

class Variable(models.Model):
visit = models.ForeignKey(Visit, on_delete=models.CASCADE, default="")
var_id = models.CharField(max_length=20, default="")
var_label = models.CharField(max_length=20, default="")

NUM = 'NUM'
INT = 'INT'
CHAR = 'CHA'
DATE = 'DAT'
BOOL = 'BOO'

DATA_TYPES = [
    (NUM, 'Numeric'),
    (INT, 'Integer'),
    (CHAR, 'Character'),
    (DATE, 'Date'),
    (BOOL, 'Boolean'),
]
widget_type = models.CharField(
    max_length=3,
    choices=DATA_TYPES,
    default=CHAR,
)

max = models.FloatField(max_length=20, default=0)
min = models.FloatField(max_length=20, default=10)
pub_date = models.DateTimeField('date published', default=timezone.now)
description = models.CharField(max_length=200, default="")
def __str__(self):
    return self.var_id

Model B (input values):

class InputValue(models.Model):
    submission = models.ForeignKey(Submission, on_delete=models.CASCADE, default="")
    variable = models.ForeignKey(Variable, on_delete=models.CASCADE, default="")
    value_char = models.CharField(max_length=128, default="NA")
    value_numeric = models.FloatField(default=0)
    value_choice = models.CharField(max_length=128, default="NA")
    def __str__(self):
        return self.variable.var_label
Back to Top