Creating a class-based REST interface
We saw how class-based views work in Flask, using the concept of pluggable views, in the Writing class-based views recipe in Chapter 4, Working with Views. In this recipe, we will now see how we can use the same to create views, which will provide a REST interface to our application.
Getting ready
Let’s take a simple view that will handle the REST-style calls to our Product
model.
How to do it...
We simply have to modify our views for product handling to extend the MethodView
class in views.py
:
import json from flask.views import MethodView class ProductView(MethodView): def get(self, id=None, page=1): if not id: products = Product.query.paginate(page, 10).items ...