我是python和flask的新手,尝试通过构建一个宁静的客户数据库来学习,所以这是在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'”错误。我错过了什么?
我很感激帮助。
答案 0 :(得分:1)
Flask-Restful期望您将它传递给flask.ext.restful.Resource
的子类,但是您传递的是一个不是Resource
的子类且不提供as_view
方法的类(来自flask.views.View
,restful.Resource
本身就是其子类。)
您需要同时制作Customer
和 CustomerList
Customers
Resource
子类:
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)
请查看the documentation's quickstart full example,了解您正在尝试做的更全面的示例。