Skip to content
Advertisement

API Project: AttributeError: ‘list’ object has no attribute ‘id’

This is my first time working with Python and APIs. I am trying to get a list of books based on an author’s name. I’m using SQLAlchemy for this and Python 3.10.2. I think this is all the relevant code:

JavaScript

Why am I getting this error:

JavaScript

And how do I fix it?

Advertisement

Answer

The all() method will return you a list of Author instances.

You cannot call the author_dict by passing a list of Author instances. This function accept only one Author instance. But you can achieve what you are looking for with a simple list comprehension

JavaScript

Another “cleaner” way is to create a to_dict method at the Author class level :

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement