返回class时的AttributeError

时间:2017-03-18 14:07:11

标签: json python-3.x attributeerror sanic

我正在使用 sanic 作为我的API,并将peewe用作ORM。

我想为json响应创建帮助器,但是我得到了错误

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班级

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

1 个答案:

答案 0 :(得分:0)

解决。

我首先需要初始化all_records变量。

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