Multiple Foreign Key Models Form Handling?

I have multiple models made up off of 5+ models, to e.g. build a price history on a product. I'm a bit confused as to how to build and validate *one* form that can display the latest price for a product, but can also easily have another price being added easily.

Do I create multiple modelforms and join them, or just a formset or should I rather statically type the form and do the validation and mapping logic in the form-save function?


Sample Models:

class Product(models.Model):

class Prices(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='prices')
primary = models.BooleanField(default=False)

class Meta:
constraints = (UniqueConstraint(fields=['product'], condition=Q(primary=True), name='unique_primary_price'),).

class Vendors(models.Model):
products = models.ManyToMany(Product, on_delete=models.CASCADE, related_name='vendors')

1 thought on “Multiple Foreign Key Models Form Handling?”

Leave a Comment