実現したいこと
クラスAのget_context_dataメソッド内に計算結果を入れた変数があるのですが、その変数をクラスBでも使いたいです。
def __init_内に定義した変数であれば別クラスで使用できることは確認済みなのですが,get_context_dataメソッド内で定義した変数もクラス外で使用出来るかが知りたいです。
発生している問題・エラーメッセージ
'ScoreAverage' object has no attribute 'total_score_avg'
該当のソースコード
views.py
1class ScoreAverage(generic.CreateView): 2 def get_context_data(self, **kwargs): 3 context = super().get_context_data(**kwargs) 4 5 #このtotal_score_avgを別クラス内でも使いたい 6 self.total_score_avg = Stat.objects.all().aggregate(Avg("total_score")) 7 context["total_score_avg"] = total_score_avg 8 return context 9 10def csv_export(request): 11 12 #csv出力の処理なので無視して頂いて大丈夫だと思います 13 response = HttpResponse(content_type='text/csv; charset=Shift-JIS') 14 filename = urllib.parse.quote((u'仮ファイル.csv').encode("utf8")) 15 response['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'{}'.format(filename) 16 writer = csv.writer(response) 17 18 #ScoreAverageクラスの変数をここで使いたい 19 score_analyze = ScoreAverage() 20 writer.writerow([score_analyze.total_score_avg]) 21 return response 22
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
よくよく考えたらget_context_dataの性質上その中の変数なんて他に渡せないですよね。。。考え直し中です。
