pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.73k stars 1.57k forks source link

Request an example using a JSON API #2149

Open dagostinelli opened 2 years ago

dagostinelli commented 2 years ago

All of the examples are using some form of ORM (or files). My use-case is a json API made with FastAPI. How about an API example?

caffeinatedMike commented 2 years ago

@dagostinelli This package is meant to be used with ORMs (hence the term ModelView). It could probably be customized to utilize an API, but you would need to override a lot of functions. And, if you're using FastAPI why opt for a package built with flask? There is a package that essentially mimics the same thing as this one, but written for FastAPI (https://github.com/fastapi-admin/fastapi-admin).

@mrjoes vetoed (can be closed)

djaysdeo commented 10 months ago

Here is a small example to use flask admin role based authorization and expose json api

class ViewWithMethodViews(flask_admin.BaseView):

    def is_accessible(self):
        return (current_user.is_active and
                current_user.is_authenticated and
                current_user.has_role('config')
        )

    @flask_admin.expose('/')
    def index(self):
        return "Hello World for /"

    @flask_admin.expose_plugview('/_api/1')
    class API_v1(MethodView):
        def get(self, cls):
            return jsonify({"Hello": "world", "method": "GET", "url":  "/_ap1/1"})

        def post(self, cls):
            return jsonify({"Hello": "world", "method": "POST", "url":  "/_ap1/1"})

admin.add_view(ViewWithMethodViews())