テンプレートプロセッサは後方追従関係を使用しました。シェルでは問題なく動作しますが、ビューではエラーが発生します。
'ParentCategory'オブジェクトには属性'postpages_set'がありません
モデル(オリジナルより少しシンプル)
class ParentCategory(models.Model):
category = models.CharField(max_length = 150)
class PostPages(models.Model):
parent_category = models.ForeignKey('ParentCategory',
blank = True,
null = True,
related_name = "parent_category")
title = models.CharField(max_length = 150)
text = models.TextField()
コンテキストプロセッサ
from shivablog.shivaapp.models import ParentCategory
def menu_items(request):
output_categories = {}
category_list = ParentCategory.objects.all()
for category in category_list:
output_categories[category] = category.postpages_set.all()
return {'output_categories': output_categories}
シェル内:
>>> output = {}
>>> cat_list = ParentCategory.objects.all()
>>> for cat in cat_list:
... output[cat] = cat.postpages_set.all()
...
>>> output
{<ParentCategory: category#1>: [<PostPages: Post 1>, <PostPages: post 2>], <ParentCategory: category #2>: [], <ParentCategory: category #3>: []}
何が問題になっていますか?このようにシェルとビューの違いは何ですか?