2016-11-12 55 views
0

我試圖使用瓶鍊金術我的數據庫查詢後返回一個JSON數據,和瓶棉花糖瓶鍊金術與棉花糖返回空JSON

然而,我的代碼不知何故總是返回空的JSON數據。

這裏是我的代碼:

筆者認爲:

@app.route('/customer/', methods=['GET']) 
def get_customer(): 
    customers = models.Customer.query.all() 
    #c = models.Customer.query.get(1) # Get Customer with an ID of 1 

    customers_schema = CustomerSchema() 

    print customers_schema.dump(customers).data 
    payload = customers_schema.dump(customers).data 
    resp = Response(response=payload, status=200, mimetype="application/json") 
    return(resp) 

我的模型:

class Customer(db.Model): 
    id = db.Column(db.Integer, primary_key=True) 
    nickname = db.Column(db.String(64), index=True, unique=True) 
    email = db.Column(db.String(120), index=True, unique=True) 
    address = db.relationship('Address', backref='customer', lazy='dynamic') 

    def __repr__(self): 
     return '<Customer %r>' % (self.nickname) 

我的架構:

class CustomerSchema(ma.ModelSchema): 
    class Meta: 
     model = Customer 

這是結果從看到控制檯:

* Debugger is active! 
* Debugger pin code: 817-774-044 
{} 
127.0.0.1 - - [12/Nov/2016 09:37:59] "GET /customer/ HTTP/1.1" 200 - 
{} 
127.0.0.1 - - [12/Nov/2016 09:41:27] "GET /customer/ HTTP/1.1" 200 - 

有什麼,我錯過?誰能幫忙?

回答

0

您可能在序列化過程中遇到了一些錯誤,您只是忽略它。試着用strict=True實例化你的模式,看看有什麼錯誤:

customers_schema = CustomerSchema(strict=True)