[英]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.我目前正在学习 Django 并问自己如何将子步骤添加到条目中。
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.感谢您在这里的辛勤工作。
You can create a Model for you recipe called RecipeModel and you can create a second model called StepsModel.您可以为您的配方创建一个名为 RecipeModel 的 Model,您可以创建另一个名为 StepsModel 的 model。 StepsModel should have a models.ForeignKey field.
StepsModel 应该有一个 models.ForeignKey 字段。 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/
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.您可以使用 JavaScript 在客户端修改您的 forms。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.