我在使用python flask框架时, 在查询数据库数据:oPropertyService = db.session.query(PropertyServiceManager).filter_by(id=psid).first()调试后出现提示错误:unable to get repr for <class 'app.model.propertyServiceManager.PropertyServiceManager'>
交互式爱情
浏览 69回答 1
1回答
侃侃尔雅
python3下用flask-sqlalchemy对mysql数据库操作案例:from flask import Flaskfrom flask.ext.sqlalchemy import SQLAlchemyapp = Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/db_name'db = SQLAlchemy(app)class User(db.Model):id = db.Column(db.Integer, primary_key=True)username = db.Column(db.String(80), unique=True)email = db.Column(db.String(120), unique=True)def __init__(self, username, email):self.username = usernameself.email = emaildef __repr__(self):return '<User %r>' % self.usernameadmin = User('admin', 'admin@example.com')db.create_all() # In case user table doesn't exists already. Else remove it.db.session.add(admin)db.session.commit() # This is needed to write the changes to databaseUser.query.all()User.query.filter_by(username='admin').first()