如何在Django模板中访问外键表的数据? [英] How to access foreign key table's data in Django templates?

查看:43
本文介绍了如何在Django模板中访问外键表的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将外键表的数据访问到Django模板中.

I want to access foreign key table's data into django templates.

我的代码如下.

class TutorialCategory(models.Model):
    tutorial_category = models.CharField(max_length=200)
    category_summary = models.CharField(max_length=200)
    category_slug = models.CharField(max_length=200, default=1)

class TutorialSeries(models.Model):
    tutorial_series = models.CharField(max_length=200)
    tutorial_category = models.ForeignKey(TutorialCategory, verbose_name="Category", on_delete=models.SET_DEFAULT)
    series_summary = models.CharField(max_length=200)


Tutorial_obj = TutorialSeries.objects.get(pk=1)
{{ Tutorial_obj.tutorial_series}}
{{Tutorial_obj.category_summary}} // Not able to access TutorialCategory

我也搜索了&发现使用了我曾经使用过的_set,但仍然无法访问表.

I have searched on SO also & found to use _set which I have used but still not able to access table.

请问有没有建议,请指导我.

Pls if anyone have suggestion pls guide me .

推荐答案

您想要的

{{Tutorial_obj.tutorial_category.category_summary}} 

不确定这是一个愚蠢的错误还是对它应该如何工作的误解

Not sure if that was just a silly error or a misunderstanding of how it's supposed to work

顺便说一句,请遵守约定:如果您坚持要求,实例实际上应该是小写的 tutorial tutorial_obj .

BTW stick to the conventions: an instance really should be lower case tutorial or tutorial_obj if you insist.

这篇关于如何在Django模板中访问外键表的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆