POST请求成功,并且数据已发布到数据库,但是我总是会收到此错误。
TypeError: Object of type User is not JSON serializable
试图将其设置为__dict__
,但随后又产生了不同的错误。如何停止此错误?
在我的schema.py文件中
from flask import Flask
from app import db, ma
import os
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(30), unique=False)
last_name = db.Column(db.String(30), unique=False)
email = db.Column(db.String(120), unique=True)
specialization = db.Column(db.String(50), unique=False)
user_type = db.Column(db.String(20), unique=False)
def __init__(self, first_name, last_name, email, specialization, user_type):
self.first_name = first_name
self.last_name = last_name
self.email = email
self.specialization = specialization
self.user_type = user_type
class UserSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ('first_name', 'last_name', 'email', 'specialization', 'user_type')
user_schema = UserSchema()
users_schema = UserSchema(many=True)
在我的app.py文件中
from flask import Flask, render_template, request, jsonify
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
@APP.route("/patient", methods=["POST"])
def add_appointment():
first_name = request.json['first_name']
last_name = request.json['last_name']
email = request.json['email']
specialization = request.json['specialization']
user_type = request.json['user_type']
new_user = schema.User(first_name, last_name, email, specialization, user_type)
schema.db.session.add(new_user)
schema.db.session.commit()
return jsonify(new_user)
我正在从树莓派上的虚拟环境中运行我的flask应用程序,并连接到Google Cloud mysql数据库(如果有任何区别)。
答案 0 :(得分:1)
似乎没有使用棉花糖模式。这可能有效,
if