簡體   English   中英

Python3“TypeError:不是在字符串格式化期間轉換的所有參數”

[英]Python3 “TypeError: not all arguments converted during string formatting”

我正在使用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

任何幫助將非常感激。

錯誤消息是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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM