Python 为什么我不能得到字段的内容?

Python 为什么我不能得到字段的内容?,python,django,Python,Django,我正在建立一个网站,在页面上显示食谱 每个页面将包含存储在单独数据库表中的信息: 使用食谱|使用配方|使用配料|遵循配方步骤 每个配方都有以下步骤: 步骤1:配料=番茄|说明=削皮切片|时间=3分钟 第二步:Ingreditant=Mozzerella |指令=wash |时间=2分钟 第三步:Ingredit=Basil |说明=清洗和准备|时间=4分钟 我希望将配方步骤与实际配方分开的原因是,我希望能够使指令字段中的内容可搜索,并为每个成分添加详细视图,并显示关于哪些指令使用最多,哪些指令使

我正在建立一个网站,在页面上显示食谱

每个页面将包含存储在单独数据库表中的信息:

使用食谱|使用配方|使用配料|遵循配方步骤

每个配方都有以下步骤:

步骤1:配料=番茄|说明=削皮切片|时间=3分钟

第二步:Ingreditant=Mozzerella |指令=wash |时间=2分钟

第三步:Ingredit=Basil |说明=清洗和准备|时间=4分钟

我希望将配方步骤与实际配方分开的原因是,我希望能够使指令字段中的内容可搜索,并为每个成分添加详细视图,并显示关于哪些指令使用最多,哪些指令使用最少的分析。我还想看看在这个细节成分查看有多少不同的配方中使用的成分

这是我到目前为止所拥有的

class cookbook(models.Model):
        title = models.CharField(max_length=255,unique=True)

class ingredient (models.Model):
        name = models.CharField(max_length=255,unique=True)


class recipesteps(models.Model):
        ingredient = models.ForeignKey(ingredient,on_delete=models.CASCADE)
        instructions = models.TextField()
        time =  models.IntegerField(default=0)

class recipe(models.Model):
        name = models.CharField(max_length=255,unique=True)
        cookbook = models.ForeignKey(cookbook,on_delete=models.CASCADE)
        ingredient_used = models.ManyToManyField(ingredient)
        recipe_steps = models.ForeignKey(recipesteps,on_delete=models.CASCADE)
        def __str__(self):
               return 'name={}   cookbook={} `'.format(self.name,self.cookbook)
我的问题是我是否遗漏了什么?是否有方法将配方中唯一的配方步骤存储在另一个数据库中,并为它们分配一个id,使其对特定配方唯一

我是否让自己的生活变得艰难?有人能告诉我一些我应该关注的资源吗

**编辑1** my views.py文件:

from django.views.generic import DetailView

class RecipeDetailView(DetailView):
     model = recipe
     def get_context_data(self, **kwargs):
         context = super(RecipeDetailView, self).get_context_data(**kwargs)
         context['instructions'] = recipesteps.objects.filter(recipe=self.get_object())
    return context
my template.html文件:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>{{ object.cookbook }}</h2>
  <h2> Recipe Name = {{ object.name }} </h2>
  <h2> Steps To Make:</h2>
  {{ instructions }} 
</body>
</html>

{{object.cookbook}
配方名称={{object.Name}
采取以下步骤:
{{指令}}
编辑2 使用上面的代码,我已经取得了真正的进步,非常感谢Paolo,现在我差不多到了,除了在模板页面上,它显示了这一点,它应该只显示字段的内容

<QuerySet [<recipesteps: name=Tomato cookbook=Tomato Cookbook <recipesteps: name=Tomato cookbook=Tomato Cookbook>]>

要展开我的评论,下面是我要做的

型号.py

  • 我将从
    配方
    模型中删除
    配方步骤
    FK
  • 配方步骤
    模型上添加
    配方
    FK
  • 可以在
    recipesteps
    中添加一个
    steps\u number
    字段,以跟踪哪个步骤先到,而不依赖
    id
  • 为什么?

    • 您不需要在
      配方
      上使用
      配方步骤
      FK,因为您希望使用
      配方
      模型来查找
      配方步骤
    而且

    • recipe\u steps
      上添加一个
      step\u number
      字段,因为您如何知道哪个步骤先到?通过
      ID
      ?如果用户希望将步骤5更改为步骤1,反之亦然,将步骤1更改为步骤strong文本,该怎么办

    我想说,
    recipesteps
    模型不应该有一个
    recipe
    外键,而不是
    recipe
    recipe\u步骤
    FK吗?因为你现在所拥有的,你不知道
    配方
    配方步骤
    属于哪一个。当我在配方步骤中输入外键时,它会声明一个未知变量Yes,因为你必须将
    配方
    放在
    配方步骤
    之前。但是,当我尝试从配方中将密钥转换为配方步骤时,我会遇到同样的问题。如果配方高于配方步骤,我需要为每个配方创建一个新的数据库配方吗?非常感谢你花时间Paolo为我回答这个问题,我了解你有什么他说并将努力实现这一点。。。我只是想弄清楚我如何拥有一个配方模板HTML文件并显示所有的配方,我不需要为每个配方创建一个新的HTML模板,并在{{recipes teps.filter}}中使用筛选条件来显示该配方的特定字段吗?你绝对可以,配方的基础是
    配方
    模型,要显示特定配方的每个步骤,必须使用
    配方
    对象过滤
    配方步骤
    模型<代码>步骤=配方步骤.对象.过滤器(配方=配方)。再次非常感谢您抽出宝贵的时间,Paolo,您希望能给我我想要的答案,我会去尝试的,谢谢会让您知道我是如何得到答案的。很抱歉,让这条线索继续下去,但在模板中有烹饪书和配料模型,因为它们只有一个字段,我将它们包括在模板中,就像这样
    {{object.cookbook}}和{object.components}
    但是当我尝试
    {{recipesteps.object.instruction}}
    时,模板中没有填充任何内容?你从哪里得到
    对象
    ?你试过
    {recipesteps.object.instruction}
    class cookbook(models.Model):
            title = models.CharField(max_length=255,unique=True)
    
    class ingredient (models.Model):
            name = models.CharField(max_length=255,unique=True)
    
    class recipe(models.Model):
            cookbook = models.ForeignKey(cookbook,on_delete=models.CASCADE)
            ingredient_used = models.ManyToManyField(ingredient)
            # Removed recipesteps FK
    
    class recipesteps(models.Model):
            step_number = models.IntegerField() # To know which step comes first.
            ingredient = models.ForeignKey(ingredient,on_delete=models.CASCADE)
            instructions = models.TextField()
            time =  models.IntegerField(default=0)
            recipe = models.ForeignKey(recipe,on_delete=models.CASCADE) #Added recipe FK