0

我将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
4

1 回答 1

0

解决了。

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

def __init__(self, model, all_records=None):
        self.all_records = all_records
        self.model = self._model_query(model)
于 2017-03-18T17:28:53.537 回答