Search code examples
pythondjangoweb-deployment

Add steps to a model entry in Django


I am currently learning Django and was asking myself how I could add substeps to an entry.

Lets take a recipe-app for example: A recipe consists of one ore many steps. The user wants to enter a recipe and gets provided with three form fields: Recipe name, ingredients and the option to add many steps.

So how can I implement the functionality to add a ingredient list of unknown length and to add a unknown number of steps?

I only know how to give the functionality of single form entries.

Thanks for your hard work here.


Solution

  • You can create a Model for you recipe called RecipeModel and you can create a second model called StepsModel. StepsModel should have a models.ForeignKey field. This way a recipe can have multiple steps and each step is related to on recipe. you can do the same with ingredients and steps. its called a many to one relationship you can read about it in documents. https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_one/

    and you can use JavaScript to modify your forms on client side.