如何在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.