'Flask API filtering posts by author

I've create CRUD API in Flask, but I have a problem with the filtering function returning books by a specific author.

from flask import Flask, request, jsonify, make_response, render_template
from flask_sqlalchemy import SQLAlchemy
from marshmallow import fields
from marshmallow_sqlalchemy import SQLAlchemySchema
from flask_marshmallow import Marshmallow
import psycopg2
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] ='postgresql+psycopg2://name:pass@localhost/testovoe1'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma=Marshmallow(app)
class Book(db.Model):
    __tablename__='books'
    id=db.Column(db.Integer, primary_key=True)
    author=db.Column(db.String(100), nullable=False)
    title=db.Column(db.String(100), nullable=False)
    def __init__(self, author, title):
        self.author=author
        self.title=title
class BookSchema(ma.Schema):
    class Meta:
        fields=("author","title")

book_schema=BookSchema()
books_schema=BookSchema(many=True)
.............
@app.route('/book_author/<author>/', methods=['GET'])
def book_authors(author):
    book=Book.query.filter_by(author=author)
    return book_schema.jsonify(book)

Suppose I want to get a list of books filtered by the author Johnson. Using the query http://127.0.0.1:5000/book_author/Johnson shows empty result.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source