我使用Flask SQLAlchemy模型为我的应用程序创建数据库,并且我尝试使用models.Posts.query.all()查询数据库 这是我的模特:
class Posts(db.Model):
post_id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(500), index=True)
date = db.Column(db.Date)
image = db.Column(db.String(500))
post_body = db.column(db.String(10000),index=True)
authors = db.relationship('Users', backref='authors', lazy='dynamic')
def __repr__(self):
return '' % (self.post_id, self.title, self.date, self.image)
这是我收到的错误消息:
>>> models.Posts.query.all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lisa-jd/Blog_platform/app/models.py", line 30, in __repr__
image = db.Column(db.String(50))
TypeError: not all arguments converted during string formatting
非常感谢任何帮助。
答案 0 :(得分:2)
错误消息是一个bitcryptic但提供了一个提示:
File "/Users/lisa-jd/Blog_platform/app/models.py", line 30, in __repr__
您的__repr__
功能:
def __repr__(self):
return '' % (self.post_id, self.title, self.date, self.image)
如果被调用,触发臭名昭着的消息就像这个简单的例子所示:
>>> "" % 10
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
TypeError: not all arguments converted during string formatting
您将3个参数传递给空格式字符串。也许你的意思是:
def __repr__(self):
return '%s: %s %s' % (self.post_id, self.title, self.date, self.image)
或str.format
def __repr__(self):
return '{}: {} {}'.format(self.post_id, self.title, self.date, self.image)