2

I have the following lazy relationship in my SQLAlchemy model:

class AuthorModel(db.Model):
    books = db.relationship("BookModel", backref="authors", lazy=True)
    def bookCount(self):
        return self.books.count()    

I would like to get books count of the author but instead I get the following error:

TypeError list.count() takes exactly one argument (0 given)

Any advice and insight is appreciated.

2
  • 2
    Maybe you should use "return len(self.books)"
    – EEEEH
    Commented Sep 29, 2021 at 3:30
  • 2
    return len(list(self.books)) wroks for me. Thanks.
    – khteh
    Commented Sep 29, 2021 at 4:23

1 Answer 1

1

here is how it is implement in SQLAlchemy:

class InstrumentedList(List[_T]):
    """An instrumented version of the built-in list."""

Hence, you should be able to treat it a list and get length as:

len(list(self.books))

Not the answer you're looking for? Browse other questions tagged or ask your own question.