Skip to main content

No project description provided

Project description

Admin App for FastAPI

Build Status Coverage License PyPi Python 3.10

Screenshots

SignIn View List View Change View

Introduction

FastAdmin is an easy-to-use Admin App for FastAPI inspired by Django Admin.

FastAdmin was built with relations in mind and admiration for the excellent and popular Django Admin. It's engraved in its design that you may configure your admin pages easiest way.

Note

FastAdmin supports only Tortoise ORM (SQLAlchemy, Pony ORM and others are in plans).

Why was FastAdmin built?

FastAPI is gaining huge popularity as an asyncio, minimalistic API framework, but there is no simple and clear system for administration your data.

Hence we started FastAdmin.

FastAdmin is designed to be minimalistic, functional, yet familiar, to ease the migration of developers wishing to switch to FastAPI from Django.

Getting Started

Installation

First you have to install FastAdmin like this:

pip install fastadmin

or using poetry

poetry install fastadmin

Quick Tutorial

Mount FastAdmin app

First of all you need to mount FastAdmin app into your app. Use prefix "/admin" as default for now. You can change it later.

Example:

from fastapi import FastAPI
from fastadmin import admin_app

...

app = FastAPI()

...

app.mount("/admin", admin_app)

...

Setup ENV variables

Setup the following env variables to configure FastAdmin (add to .env or export them like on example):

Example:

export ADMIN_USER_MODEL = User
export ADMIN_USER_MODEL_USERNAME_FIELD = username
export ADMIN_SECRET_KEY = secret_key
  • ADMIN_USER_MODEL - a name of your User model (has to be registered in FastAdmin later)
  • ADMIN_USER_MODEL_USERNAME_FIELD - an username field (unique field from your user table) for authentication (could be email, or phone)
  • ADMIN_SECRET_KEY - a secret key (generate a strong secret key and provide here)

Implement admin_authenticate class method

Add admin_authenticate class method to your User model (we need it for authentication):

Example for Tortoise ORM:

import bcrypt
from typing import Any
from tortoise.models import Model


class User(Model):
    username = fields.CharField(max_length=255, unique=True)
    hash_password = fields.CharField(max_length=255)
    is_superuser = fields.BooleanField(default=False)

    ...

    @classmethod
    async def admin_authenticate(cls, username: str, password: str) -> Any | None:
        user = await cls.filter(username=username, is_superuser=True).first()
        if not user:
            return None
        if not bcrypt.checkpw(password.encode(), user.password_hash.encode()):
            return None
        return user

Register your ORM models

For Tortoise ORM (we support only Tortoise ORM for now):

from tortoise.models import Model

from fastadmin import TortoiseModelAdmin, register


class User(Model):
    ...


@register(User)
class UserAdmin(TortoiseModelAdmin):
    pass

Enjoy

Run your project (see https://fastapi.tiangolo.com/tutorial/first-steps/):

uvicorn ...

Go to http://localhost:8000/admin

Configuration

You can find all env variables to configure FastAdmin here

You can find all parameters and methods to configure your ModelAdmin classes here

Example:

@register(User)
class UserAdmin(TortoiseModelAdmin):
    exclude = ("password_hash",)
    list_display = ("id", "username")

    def has_delete_permission(self) -> bool:
        return False

Other ORMs or own implementation

We are going to support SQLAlchemy and Pony ORM soon...

If you have smth else (your own implementation of ORM and so on) you will may overload ModelAdmin class and implement the following interfaces

from fastadmin import ModelAdmin, register, WidgetType

class MyModelAdmin(ModelAdmin):
    async def save_model(self, obj: Any, payload: dict, add: bool = False) -> None:
        raise NotImplementedError

    async def delete_model(self, obj: Any) -> None:
        raise NotImplementedError

    async def get_obj(self, id: str) -> Any | None:
        raise NotImplementedError

    async def get_list(
        self,
        offset: int | None = None,
        limit: int | None = None,
        search: str | None = None,
        sort_by: str | None = None,
        filters: dict | None = None,
    ) -> tuple[list[Any], int]:
        raise NotImplementedError

    async def get_export(
        self,
        format: ExportFormat | None,
        offset: int | None = None,
        limit: int | None = None,
        search: str | None = None,
        sort_by: str | None = None,
        filters: dict | None = None,
    ) -> StringIO | BytesIO | None:
        raise NotImplementedError

    def get_form_widget(self, field: str) -> tuple[WidgetType, dict]:
        raise NotImplementedError

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

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

Source Distribution

fastadmin-0.1.4.tar.gz (1.9 MB view hashes)

Uploaded Source

Built Distribution

fastadmin-0.1.4-py3-none-any.whl (1.9 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page