Package: fastadmin
Admin Dashboard App for FastAPI/Flask/DjangoScreenshotsIntroductionFastAdminis an easy-to-use Admin Dashboard App for FastAPI/Django/Flask 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 dashboard for FastAPI/Django/Flask easiest way.FastAdmin is designed to be minimalistic, functional and yet familiar.Getting StartedIf you have any questions that are beyond the scope of the documentation, Please feel free to emailus.InstallationFollow the steps below to setup FastAdmin:Install the package using pip:Note: For zsh and macos use:pip install fastadmin[fastapi,django]pipinstallfastadmin[fastapi,django]# for fastapi with django ormpipinstallfastadmin[fastapi,tortoise-orm]# for fastapi with tortoise ormpipinstallfastadmin[fastapi,pony]# for fastapi with pony ormpipinstallfastadmin[fastapi,sqlalchemy]# for fastapi with sqlalchemy ormpipinstallfastadmin[django]# for django with django ormpipinstallfastadmin[django,pony]# for django with pony ormpipinstallfastadmin[flask,sqlalchemy]# for flask with sqlalchemyInstall the package using poetry:poetryadd'fastadmin[fastapi,django]'# for fastapi with django ormpoetryadd'fastadmin[fastapi,tortoise-orm]'# for fastapi with tortoise ormpoetryadd'fastadmin[fastapi,pony]'# for fastapi with pony ormpoetryadd'fastadmin[fastapi,sqlalchemy]'# for fastapi with sqlalchemy ormpoetryadd'fastadmin[django]'# for django with django ormpoetryadd'fastadmin[django,pony]'# for django with pony ormpoetryadd'fastadmin[flask,sqlalchemy]'# for flask with sqlalchemyConfigure required settings using virtual environment variables:Note: You can add these variables to .env and use python-dotenv to load them. See all settingshereexportADMIN_USER_MODEL=UserexportADMIN_USER_MODEL_USERNAME_FIELD=usernameexportADMIN_SECRET_KEY=secret_keyQuick TutorialSetup FastAdmin for a frameworkFastAPIfromfastapiimportFastAPIfromfastadminimportfastapi_appasadmin_appapp=FastAPI()app.mount("/admin",admin_app)Djangofromdjango.urlsimportpathfromfastadminimportget_django_admin_urlsasget_admin_urlsfromfastadmin.settingsimportsettingsurlpatterns=[path(f"{settings.ADMIN_PREFIX}/",get_admin_urls()),]FlaskfromflaskimportFlaskfromfastadminimportflask_appasadmin_appapp=Flask(__name__)app.register_blueprint(admin_app,url_prefix="/admin")Register ORM modelsTortoise ORMfromuuidimportUUIDimportbcryptfromtortoiseimportfieldsfromtortoise.modelsimportModelfromfastadminimportTortoiseModelAdmin,registerclassUser(Model):username=fields.CharField(max_length=255,unique=True)hash_password=fields.CharField(max_length=255)is_superuser=fields.BooleanField(default=False)is_active=fields.BooleanField(default=False)def__str__(self):returnself.username@register(User)classUserAdmin(TortoiseModelAdmin):exclude=("hash_password",)list_display=("id","username","is_superuser","is_active")list_display_links=("id","username")list_filter=("id","username","is_superuser","is_active")search_fields=("username",)asyncdefauthenticate(self,username:str,password:str)->UUID|int|None:user=awaitUser.filter(username=username,is_superuser=True).first()ifnotuser:returnNoneifnotbcrypt.checkpw(password.encode(),user.hash_password.encode()):returnNonereturnuser.idDjango ORMfromdjango.dbimportmodelsfromfastadminimportDjangoModelAdmin,registerclassUser(models.Model):username=models.CharField(max_length=255,unique=True)hash_password=models.CharField(max_length=255)is_superuser=models.BooleanField(default=False)is_active=models.BooleanField(default=False)def__str__(self):returnself.username@register(User)classUserAdmin(DjangoModelAdmin):exclude=("hash_password",)list_display=("id","username","is_superuser","is_active")list_display_links=("id","username")list_filter=("id","username","is_superuser","is_active")search_fields=("username",)defauthenticate(self,username,password):obj=User.objects.filter(username=username,is_superuser=True).first()ifnotobj:returnNoneifnotobj.check_password(password):returnNonereturnobj.idSQL AlchemyimportbcryptfromsqlalchemyimportBoolean,Integer,String,selectfromsqlalchemy.ext.asyncioimportasync_sessionmaker,create_async_enginefromsqlalchemy.ormimportDeclarativeBase,Mapped,mapped_columnfromfastadminimportSqlAlchemyModelAdmin,registersqlalchemy_engine=create_async_engine("sqlite+aiosqlite:///:memory:",echo=True,)sqlalchemy_sessionmaker=async_sessionmaker(sqlalchemy_engine,expire_on_commit=False)classBase(DeclarativeBase):passclassUser(Base):__tablename__="user"id:Mapped[int]=mapped_column(Integer,primary_key=True,nullable=False)username:Mapped[str]=mapped_column(String(length=255),nullable=False)hash_password:Mapped[str]=mapped_column(String(length=255),nullable=False)is_superuser:Mapped[bool]=mapped_column(Boolean,default=False,nullable=False)is_active:Mapped[bool]=mapped_column(Boolean,default=False,nullable=False)def__str__(self):returnself.username@register(User,sqlalchemy_sessionmaker=sqlalchemy_sessionmaker)classUserAdmin(SqlAlchemyModelAdmin):exclude=("hash_password",)list_display=("id","username","is_superuser","is_active")list_display_links=("id","username")list_filter=("id","username","is_superuser","is_active")search_fields=("username",)asyncdefauthenticate(self,username,password):sessionmaker=self.get_sessionmaker()asyncwithsessionmaker()assession:query=select(User).filter_by(username=username,password=password,is_superuser=True)result=awaitsession.scalars(query)user=result.first()ifnotuser:returnNoneifnotbcrypt.checkpw(password.encode(),user.hash_password.encode()):returnNonereturnuser.idPony ORMimportbcryptfrompony.ormimportDatabase,PrimaryKey,Required,db_sessionfromfastadminimportPonyORMModelAdmin,registerdb=Database()db.bind(provider="sqlite",filename=":memory:",create_db=True)classUser(db.Entity):# type: ignore [name-defined]_table_="user"id=PrimaryKey(int,auto=True)username=Required(str)hash_password=Required(str)is_superuser=Required(bool,default=False)is_active=Required(bool,default=False)def__str__(self):returnself.username@register(User)classUserAdmin(PonyORMModelAdmin):exclude=("hash_password",)list_display=("id","username","is_superuser","is_active")list_display_links=("id","username")list_filter=("id","username","is_superuser","is_active")search_fields=("username",)@db_sessiondefauthenticate(self,username,password):user=next((fforfinself.model_cls.select(username=username,password=password,is_superuser=True)),None)ifnotuser:returnNoneifnotbcrypt.checkpw(password.encode(),user.hash_password.encode()):returnNonereturnuser.idDocumentationSee full documentationhere.LicenseThis project is licensed under the MIT License - see theLICENSEfile for details.