1 year ago
#387548
farmacreds
how to resolve AttributeError: 'list' object has no attribute 'get'
I have created a database and I am trying to POST a "book" through Postman. The book should have a bunch of attributes such as title, author, rating, id, description etc. The database seems to work and I can, for example, get information about the logged in user through Postman.
When I try to POST a book I get the error as the headline; AttributeError: 'list' object has no attribute 'get'
but I can't understand what 'list' the error is referring to..?
Here is the code for the route;
@app.route('/books', methods=['GET', 'POST'])
def books():
if Book.query.all() == None:
abort(404)
if request.method == 'GET':
# Handle GET-request
books = []
for x in Book.query.all():
books.append(Book.serialize(x))
return jsonify(books)
elif request.method == 'POST':
input_data = request.get_json(force=True)
if not input_data.get('title', False):
abort(400)
if not input_data.get('author', False):
abort(400)
if not input_data.get('genre', False):
abort(400)
if not input_data.get('publish_year', False):
abort(400)
if not input_data.get('image', False):
abort(400)
if not input_data.get('current_book', False):
abort(400)
title_input = input_data.get('title')
author_input = input_data.get('author')
genre_input = input_data.get('genre')
short_description_input = input_data.get('short_description')
long_description_input = input_data.get('long_description')
publish_year_input = input_data.get('publish_year')
image_input = input_data.get('image')
rating_input = input_data.get('rating')
current_book_input = input_data.get('current_book')
input_book = Book(title=title_input, author=author_input,
genre=genre_input, short_description=short_description_input,
long_description=long_description_input,
publish_year=publish_year_input, image=image_input,
rating=rating_input, current_book=current_book_input)
db.session.add(input_book)
db.session.commit()
return jsonify(Book.serialize(input_book))
and the postman POST request
[
{
"title":"Here goes the title",
"author":"Best author",
"current_book":true,
"genre": "Classic",
"id": 1,
"image": "pictures/bookpicture1.png",
"long_description": "reaaaally good book)",
"publish year": 1803,
"rating": 3.5,
"short_description": "read it!!"
}
]
python
post
postman
0 Answers
Your Answer