为什么我无法获取字段的内容?

时间:2019-11-11 18:58:53

标签: python django

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

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

使用的食谱|食谱使用|使用的原料|食谱步骤

每个食谱都有要遵循的步骤:

步骤1:成分=番茄|指令=去皮和切片|时间= 3分钟

第2步:原料= Mozzerella |指令=洗涤|时间= 2分钟

第3步:配料=罗勒|说明=清洗并准备|时间= 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 **

我的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

我的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>

编辑2

使用上面的代码,我已经取得了真正的进步,非常感谢Paolo,现在我几乎在那儿了,除了在模板页面上显示了它应该只显示字段内容的地方

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

这显示的是我views.py的str(self),而不是字段的内容是我使用self.get_object()命令做错了吗?

1 个答案:

答案 0 :(得分:0)

要展开我的评论,这就是我要做的事情。

models.py

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
  1. 我将从recipesteps模型中删除recipe FK
  2. recipe模型上添加一个recipesteps FK
  3. 也许可以在steps_number中添加一个recipesteps字段来跟踪哪个步骤先行而不依赖于id

为什么?

  • 您不需要recipesteps上的recipe FK ,因为您想使用recipe模型来寻找recipesteps周围。

  • step_number上添加一个recipe_steps字段,因为您如何知道接下来哪个步骤呢?通过ID?如果用户想将步骤 5 例如更改为步骤 1 ,反之亦然,将步骤 1 更改为步骤 strong ,该怎么办? >?