4

I'm using Flask SQLAlchemy models to make a database for my app, and I'm trying to query the database using models.Posts.query.all() This is my model:

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)

And this is the error message I get:

>>> 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

Any help would be very appreciated.

1
  • Post the whole model here. This line image = db.Column(db.String(50)) is missing. ` Commented Dec 2, 2017 at 10:08

1 Answer 1

3

The error message is a bitcryptic but gives an hint:

File "/Users/lisa-jd/Blog_platform/app/models.py", line 30, in __repr__

your __repr__ function:

def __repr__(self):
    return '' % (self.post_id, self.title, self.date, self.image)

if called, triggers the infamous message like this simple example shows:

>>> "" % 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

you're passing 3 arguments to an empty formatting string. Maybe you meant something like:

def __repr__(self):
    return '%s: %s %s' % (self.post_id, self.title, self.date, self.image)

or with str.format

def __repr__(self):
    return '{}: {} {}'.format(self.post_id, self.title, self.date, self.image)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.