0

我是 python 和烧瓶的新手,尝试通过构建一个安静的客户数据库来学习,所以这是在 dataModels.py 中:

很朴实:

class Customer(object):
    def __init__(self, pID, name, address, email, custNO, agent ):
        self.pID = pID
        self.name = name
        self.address =address
        self.email = email
        self.custNO = custNO
        self.agent = agent

class CustomerList(list):
    def addCustomer(self, pID, name, address, email, custNO, agent):
        self.append((false, pID, name, address, email, custNO, agent))
    def custCount(self):
        return len (self)

这是在views.py中:

api.add_resource(CustomerList, '/customer')

我收到“AttributeError:类型对象'CustomerList'没有属性'as_view'”错误。我错过了什么?

我很感激帮助。

4

1 回答 1

1

Flask-Restful expects that you will pass it a subclass of flask.ext.restful.Resource but you are passing it a class that is not a subclass of Resource and which does not provide an as_view method (which comes from flask.views.View, which restful.Resource is itself a subclass of).

You will want to make both Customer and CustomerList Customers Resource sub-classes:

class Customer(Resource):
    def __init__(self, p_id, name, address, email, customer_number, agent):
        self.p_id = p_id
        self.name = name
        self.address = address
        self.email = email
        self.customer_number = customer_number
        self.agent = agent

class Customers(Resource):
    def __init__(self, *args, **kwargs):
        super(Customers, self).__init__(*args, **kwargs)
        self._customer_list = []

    def add_customer(self, p_id, name, address, email, customer_number, agent):
        customer = Customer(p_id, name, address, email, customer_number, agent)
        self._customer_list.append(customer)

    def __len__(self):
        return len(self._customer_list)

Take a look at the documentation's quickstart full example for a fuller example of what you are trying to do.

于 2014-10-22T20:17:25.797 回答