【问题标题】:Django foreign key relationDjango外键关系
【发布时间】:2016-07-29 16:06:30
【问题描述】:

我最近开始使用 Python 和 Django 制作网站,但遇到了一个我无法弄清楚的问题。

你可以肯定,这是因为我完全没有经验,而且我忽略了一些简单的事情或做了一些愚蠢的事情,但是哦,好吧..

在我的网站上,我有一个页面用于显示有关食谱的详细信息(描述、成分、说明等)。我是这样显示的:

views.py
class RecipeView(generic.DetailView):
    model = recipe
    template_name = 'whatsfordinner/recipe.html'
    context_object_name = 'details'

到目前为止,我已经能够显示我的单个信息,没问题 ({{details.whatever}})

我的问题是指令和成分都作为外键关系存储在我的数据库中,因此需要以不同的方式输出。我的数据库如下所示:

class recipe(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(default="No decsription added")
    image = models.ImageField(upload_to='images/',
                                        default='images/default.jpg')
    total_favourites = models.IntegerField()
    servings = models.IntegerField()

    def __str__(self):
        return self.title

class ingredients(models.Model):
    recipe = models.ForeignKey(recipe)
    ingredient = models.CharField(max_length=255)

我很难为我选择的食谱输出相关成分,我希望得到一些指点。

【问题讨论】:

  • 输出不同,怎么样?请准确说明您正在尝试什么以及为什么会出错。
  • recipeobj.ingredients_set.all()

标签: python mysql django database foreign-key-relationship


【解决方案1】:

对于配方对象,说r

r.ingredients_set.all() 将列出与该食谱相关的所有成分。

您可以对此进行进一步过滤: r.ingredients_set.filter(title__startswith='clover')

Django 文档中有一个综合指南: https://docs.djangoproject.com/en/1.9/topics/db/examples/many_to_one/

假设你想在你的views.py中列出一个食谱的所有成分:

ingredient_list = r.ingredients_set.all()

然后将您的上下文字典中的成分列表传递给您的模板。如果您不知道上下文字典是什么,您在做什么,请阅读 Django 文档!很多人都付出了很多努力来创建出色的文档。

假设context['ingredients'] = ingredient_list,其中context 是您传递给模板html 的上下文字典。 然后在你的模板中,使用 Django 模板语言来做这样的事情:

{% for i in ingredients %}
  <p>{{ i.ingredient }}</p>
{% endfor %}

这里ingredients 是您使用上下文字典传递的ingredient_list,对于for 循环中的每个成分对象i,您将显示&lt;ingredient object i&gt;.ingredient 的值

这里是官方教程的链接,如果有帮助的话。 https://docs.djangoproject.com/en/1.9/intro/tutorial01/

【讨论】:

  • 是的,如果我在 manage.py shell 中测试它并且只是硬编码一个配方 id 就可以了。但是我不明白我是如何设法将其合并到我的 view.py 和我的模板中的。
  • 在您的模板中,执行 for 循环,例如 for i in ingredients_list:阅读 django 模板教程以获取示例
【解决方案2】:

请看这里:https://docs.djangoproject.com/en/1.9/topics/class-based-views/generic-display/#adding-extra-context

class PublisherDetail(DetailView):

    model = Publisher


class RecipeView(generic.DetailView):
    model = recipe
    template_name = 'whatsfordinner/recipe.html'
    context_object_name = 'details'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(RecipeView, self).get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['ingredient_list'] = recipe.ingredients_set.all()
        return context

以上内容未经测试

【讨论】:

  • 这似乎确实..几乎奏效了。当我按照链接的文档不小心放置 ingredients.objects.all() 时,它会提取所有成分。当我更改它时 recipe.ingredients_set.all() 它只会抛出一个错误:'ReverseManyToOneDescriptor' object has no attribute 'all'
猜你喜欢
  • 2013-07-20
  • 2020-01-02
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多