接收空对象列表作为 JSON。烧瓶架 API
Receive a list of empty objects as a JSON. Flask Rest API
我正在尝试使用烧瓶来休息 API。我使用 sqlite 作为我的数据库,SQLAlchemy 作为我的对象关系映射器,marshmallow 作为我的序列化库。我成功地为客户 table 创建了 POST 映射 API。但是当我尝试像这样构建获取所有客户端点时,
@app.route('/customers', methods=['GET'])
def get_customers():
all_users = Customer.query.all()
result = customer_schema.dump(all_users)
return jsonify(result.data)
我收到一个错误 AttributeError: 'list' object has no attribute 'data'
我用 解决了这个问题。
现在我的代码是这样的。
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False;
database = SQLAlchemy(app)
ORM = Marshmallow(app)
#Models
##Customer Model
class Customer(database.Model):
cus_id = database.Column(database.Integer, primary_key=True)
first_name = database.Column(database.String(50))
last_name = database.Column(database.String(50))
address = database.Column(database.String(100))
email = database.Column(database.String(100))
nic = database.Column(database.String(10))
telephone = database.Column(database.String(10))
def __init__(self, first_name, last_name, address, email, nic, telephone):
self.first_name = first_name
self.last_name = last_name
self.address = address
self.email = email
self.nic = nic
self.telephone = telephone
##Customer Schema
class CustomerSchema(ORM.Schema):
class Meta:
Model = Customer
#Initialize Customer Schema
customer_schema = CustomerSchema(many=True)
#customer API
##Register a new customer
@app.route('/register', methods=['POST'])
def add_customer():
first_name = request.json['first_name']
last_name = request.json['last_name']
address = request.json['address']
email = request.json['email']
nic = request.json['nic']
telephone = request.json['telephone']
new_customer = Customer(first_name, last_name, address, email, nic, telephone)
database.session.add(new_customer)
database.session.commit()
return customer_schema.jsonify(new_customer)
##Get all customers
@app.route('/customers', methods=['GET'])
def get_customers():
all_users = Customer.query.all()
result = customer_schema.dump(all_users)
return jsonify(result)
#start the server
if __name__ == '__main__':
app.run(debug=True)
但是当我调用我的 GET 请求时,我只得到一个空对象列表。(我的客户 table 有 12 个客户,我得到 12 个空对象)
According to flask-marshmallow document,您应该修改这部分代码
##Customer Schema
class CustomerSchema(ORM.Schema):
class Meta:
fields = ("cus_id", "first_name", "last_name", "address", "email", "nic", "telephone")
# Model = Customer
我正在尝试使用烧瓶来休息 API。我使用 sqlite 作为我的数据库,SQLAlchemy 作为我的对象关系映射器,marshmallow 作为我的序列化库。我成功地为客户 table 创建了 POST 映射 API。但是当我尝试像这样构建获取所有客户端点时,
@app.route('/customers', methods=['GET'])
def get_customers():
all_users = Customer.query.all()
result = customer_schema.dump(all_users)
return jsonify(result.data)
我收到一个错误 AttributeError: 'list' object has no attribute 'data'
我用
现在我的代码是这样的。
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False;
database = SQLAlchemy(app)
ORM = Marshmallow(app)
#Models
##Customer Model
class Customer(database.Model):
cus_id = database.Column(database.Integer, primary_key=True)
first_name = database.Column(database.String(50))
last_name = database.Column(database.String(50))
address = database.Column(database.String(100))
email = database.Column(database.String(100))
nic = database.Column(database.String(10))
telephone = database.Column(database.String(10))
def __init__(self, first_name, last_name, address, email, nic, telephone):
self.first_name = first_name
self.last_name = last_name
self.address = address
self.email = email
self.nic = nic
self.telephone = telephone
##Customer Schema
class CustomerSchema(ORM.Schema):
class Meta:
Model = Customer
#Initialize Customer Schema
customer_schema = CustomerSchema(many=True)
#customer API
##Register a new customer
@app.route('/register', methods=['POST'])
def add_customer():
first_name = request.json['first_name']
last_name = request.json['last_name']
address = request.json['address']
email = request.json['email']
nic = request.json['nic']
telephone = request.json['telephone']
new_customer = Customer(first_name, last_name, address, email, nic, telephone)
database.session.add(new_customer)
database.session.commit()
return customer_schema.jsonify(new_customer)
##Get all customers
@app.route('/customers', methods=['GET'])
def get_customers():
all_users = Customer.query.all()
result = customer_schema.dump(all_users)
return jsonify(result)
#start the server
if __name__ == '__main__':
app.run(debug=True)
但是当我调用我的 GET 请求时,我只得到一个空对象列表。(我的客户 table 有 12 个客户,我得到 12 个空对象)
According to flask-marshmallow document,您应该修改这部分代码
##Customer Schema
class CustomerSchema(ORM.Schema):
class Meta:
fields = ("cus_id", "first_name", "last_name", "address", "email", "nic", "telephone")
# Model = Customer