How to build a form in django based on 3 models with Many-To-Many relationship
This is my first experience with django and I'm stuck on creating the form. The models that must be involved in the form are the following:
models.py
class Ingredient(models.Model):
name = models.CharField(max_length=100)
id_category = models.ForeignKey(Category, on_delete=models.CASCADE)
property = YAMLField()
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Ingredients"
class Category(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Categories"
class Inventory(models.Model):
id_user = models.ManyToManyField(User)
name = models.CharField(max_length=200)
last_update = models.DateTimeField(auto_now=True)
creation_date = models.DateTimeField(auto_now_add=True)
ingredients = models.ManyToManyField(Ingredient, through='InventoryIngredient')
class Meta:
verbose_name_plural = 'Inventories'
ordering = ['-creation_date']
class InventoryIngredient(models.Model):
id_ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
id_inventory = models.ForeignKey(Inventory, on_delete=models.CASCADE)
quantity = models.DecimalField(decimal_places=2, max_digits=10)
measurement_unit = models.CharField(max_length=100)
expiry_date = models.DateField()
I don't know where to start implementing the form. I tried to look for something similar but I didn't find anything about form management in case of many-to-many relationship with a through table.
In practice, in my form there will have to be the attributes of the Ingredient(name, property) model and the attributes of the InventoryIngredient(quantity, measurement_unit, expiry_date) model and also the name for the Category.
I ask you if you can please help me with this, at least I would need a basis from which to implement my form.