emraldinho
emraldinho

Reputation: 497

Restful Flask Python

I am new to python and flask, trying learning by building a restful customer database, so this is in dataModels.py:

very plain:

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)

this is in views.py:

api.add_resource(CustomerList, '/customer')

I am getting a "AttributeError: type object 'CustomerList' has no attribute 'as_view'" error. What am I missing?

I appreciate the help.

Upvotes: 0

Views: 729

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 159855

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.

Upvotes: 1

Related Questions