0

My models:

class Ingredient(models.Model):
    BASE_UNIT_CHOICES = [("g", "Grams"), ("ml", "Mililiters")]
    CURRENCY_CHOICES = [("USD", "US Dollars"), ("EUR", "Euro")]

    ingredient_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    base_unit = models.CharField(max_length=2, choices=BASE_UNIT_CHOICES)
    cost_per_base_unit = models.FloatField()
    currency = models.CharField(
        max_length=3, choices=CURRENCY_CHOICES, default="EUR")

    def __str__(self):
        return self.name


class RecipeIngredient(models.Model):
    quantity = models.FloatField()
    ingredient_id = models.ForeignKey(Ingredient, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.quantity} / {self.ingredient_id}"


class Recipe(models.Model):
    recipe_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    ingredients = models.ManyToManyField(RecipeIngredient)
    date_created = models.DateTimeField('Date Created')

    def __str__(self):
        return f"{self.name}, {self.ingredients}"

When I use the admin page, it has this + button that allows me to create new ingredient/quantity combinations

like this

But when I try to use it from a form in my code it looks like this

Here is my form code:

class AddRecipeForm(forms.ModelForm):
    class Meta:
        model = Recipe
        fields = ['name', 'ingredients', 'date_created']
Guillermo
  • 7
  • 2

1 Answers1

0

You should write the 'widgets' for each field in you Form that need configuration.

Check the documentation 'Widgets in forms', or even, you can define your own Widgets.

Jony_23
  • 322
  • 4
  • 12