Skip to main content

Reusable CRUD endpoints using flask-smorest, flask_jwt_extended, and SQLAlchemy.

Project description

Flask Smorest CRUD

Why repeat yourself?

What is this?

Right now, a work in progress.

This library aims to tie together Flask-SQLAlchemy and Flask-Smorest to implement a sane default but easily customizable CRUD API based on SQLAlchemy models inside of Flask.


In create_app():

from smorest_crud import CRUD
from flask_jwt_extended import JWTManager, get_current_user

app = Flask()


CRUD View:

from flask_smorest import Blueprint
from smorest_crud import ResourceView, CollectionView

pet_blp = Blueprint("pets", "pets", url_prefix="/pet")

class PetCollection(CollectionView):
    model = Pet
    prefetch = [Pet.human, (Pet.human,]  # joinedload
    access_checks_enabled = False

    create_enabled = True
    list_enabled = True

    def get(self):
        query = super().get()
        return query.filter_by(name='mischa')

    def post(self, args):
        return super().post(args)

class PetResource(ResourceView):
    model = Pet

    access_checks_enabled = True
    get_enabled = True
    update_enabled = True
    delete_enabled = True

    def get(self, pk):
        return super().get(pk)

    def patch(self, args, pk):
        return super().patch(args, pk)

    def delete(self, pk):
        return super().delete(pk)

Who is this for?

This library is only useful if your application uses:

Where to look?

Look at smorest_crud/test/

Feedback and comments welcome. What would you like to see? Open an issue or a PR with your thoughts.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for smorest-crud, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size smorest-crud-0.0.3.tar.gz (4.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page