2017-03-18 5 views
0

を返却はAttributeError、およびORMなどpeewe。私は私のAPIのため<strong>sanic</strong>を使用していたクラス

と私はJSONレスポンスのためのヘルパーを作成したいが、私は

from sanic.response import json 


class JsonResponse: 
    def __init__(self, model, all_records=None): 
     self.model = self._model_query(model) 
     self.all_records = all_records 

    def _model_query(self, model): 
     if self.all_records: 
      records = json({model: list(model)}) 
     else: 
      records = {} 

     return records 

答えて

0

が解決エラー

AttributeError: 'JsonResponse' object has no attribute 'all_records'

get方法コントローラ

from sanic.response import json 
from sanic.views import HTTPMethodView 

from models.project import Project 
from helpers.json import JsonResponse 


class ProjectListResource(HTTPMethodView): 
    def get(self, resp): 
     projects = Project().select().dicts() 
     return JsonResponse(projects, all_records=True) 

で、私のhelperクラスを得ました。

最初に変数all_recordsを初期化する必要がありました。

def __init__(self, model, all_records=None): 
     self.all_records = all_records 
     self.model = self._model_query(model) 
関連する問題