fastapi-admin实现
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>集思广益</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">FastAPI操作数据库的<span style="color: black;">文案</span>,网上有<span style="color: black;">非常多</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">FastAPI因其原生支持异步,且支持Pydantic标准格式校验,广泛受到<span style="color: black;">研发</span>者的<span style="color: black;">喜爱</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">那样</span><span style="color: black;">怎样</span>支持异步数据库?并继续支持Pydantic呢?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">那样</span>FastAPI能<span style="color: black;">不可</span>像Django那样支持 </span><strong style="color: blue;"><span style="color: black;">admin</span></strong><span style="color: black;">后台呢?<span style="color: black;">能够</span>快捷的对数据库表进行CRUD呢?</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/6b49c4be719b4b3c9f6382ef14f75a8e~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728742974&x-signature=bazk3ZZjs%2BobvqL2tTqrhWfoDw8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">2、</span>SQLModel</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/816585d00e6f414b863d252f097f8ebf~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728742974&x-signature=OLH%2BN8e1R2TKutN7tWMRWVOpJOA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这里</span>,<span style="color: black;">咱们</span><span style="color: black;">运用</span>了 </span><strong style="color: blue;"><span style="color: black;">SQLModel</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SQLModel 是一个库,用于<span style="color: black;">经过</span> Python 代码和 Python 对象与SQL 数据库进行交互。它被设计为直观、易于<span style="color: black;">运用</span>、高度兼容和强大。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">SQLModel</span></strong><span style="color: black;">基于 Python 类型注释,由Pydantic和SQLAlchemy<span style="color: black;">供给</span>支持。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重点</span>特点是:</span></p><strong style="color: blue;"><span style="color: black;">编写直观</span></strong>:强大的编辑器支持。处处圆满。更少的调试时间。旨在易于<span style="color: black;">运用</span>和学习。阅读文档的时间更少。<strong style="color: blue;"><span style="color: black;">易于<span style="color: black;">运用</span></span></strong>:它<span style="color: black;">拥有</span><span style="color: black;">恰当</span>的默认值,并在底层做了<span style="color: black;">非常多</span>工作以简化您编写的代码。<strong style="color: blue;"><span style="color: black;">兼容</span></strong>:它旨在与<strong style="color: blue;"><span style="color: black;">FastAPI</span></strong>、Pydantic 和 SQLAlchemy 兼容。<strong style="color: blue;"><span style="color: black;">可扩展</span></strong>:您<span style="color: black;">持有</span> SQLAlchemy 和 Pydantic 的所有功能。<strong style="color: blue;"><span style="color: black;">简短</span></strong>:最小化代码重复。单个类型注解<span style="color: black;">能够</span>完成<span style="color: black;">非常多</span>工作。无需在 SQLAlchemy 和 Pydantic 中复制模型。<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置示例代码:db_config.py</h1>
<span style="color: black;">from</span> sqlalchemy.orm <span style="color: black;">import</span> declarative_base
<span style="color: black;">from</span> sqlmodel <span style="color: black;">import</span>create_engine
sqlite_file_name =<span style="color: black;">"alert.db"</span>
sqlite_url = <span style="color: black;">f"sqlite:///<span style="color: black;">{sqlite_file_name}</span>"</span>
<span style="color: black;"># mysql or sqlite all is ok </span>
mysql_url = <span style="color: black;">mysql+mysqlconnector://root:mysql@127.0.0.1:3306/alert_test_db</span>
engine = create_engine(mysql_url, echo=<span style="color: black;">True</span>)
Base = declarative_base()
<h1 style="color: black; text-align: left; margin-bottom: 10px;">模型示例代码:db_models.py</h1><span style="color: black;">from</span> typing <span style="color: black;">import</span>List, Optional<span style="color: black;">from</span> sqlmodel <span style="color: black;">import</span> Field, Relationship, Session, SQLModel, create_engine
<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">HeroTeamLink</span><span style="color: black;">(SQLModel, table=True)</span>:</span>team_id: Optional = Field(
default=<span style="color: black;">None</span>, foreign_key=<span style="color: black;">"team.id"</span>, primary_key=<span style="color: black;">True</span>
)
hero_id: Optional = Field(
default=<span style="color: black;">None</span>, foreign_key=<span style="color: black;">"hero.id"</span>, primary_key=<span style="color: black;">True</span>
)
<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Team</span><span style="color: black;">(SQLModel, table=True)</span>:</span>
id: Optional = Field(default=<span style="color: black;">None</span>, primary_key=<span style="color: black;">True</span>)
name: str = Field(index=<span style="color: black;">True</span>)
headquarters: str
heroes: List[<span style="color: black;">"Hero"</span>] = Relationship(back_populates=<span style="color: black;">"teams"</span>, link_model=HeroTeamLink)
<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Hero</span><span style="color: black;">(SQLModel, table=True)</span>:</span>id: Optional = Field(default=<span style="color: black;">None</span>, primary_key=<span style="color: black;">True</span>)
name: str = Field(index=<span style="color: black;">True</span>)
secret_name: str
age: Optional = Field(default=<span style="color: black;">None</span>, index=<span style="color: black;">True</span>)
teams: List = Relationship(back_populates=<span style="color: black;">"heroes"</span>, link_model=HeroTeamLink)
sqlite_file_name = <span style="color: black;">"database.db"</span>
sqlite_url = <span style="color: black;">f"sqlite:///<span style="color: black;">{sqlite_file_name}</span>"</span>
engine = create_engine(sqlite_url, echo=<span style="color: black;">True</span>)
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">create_db_and_tables</span><span style="color: black;">()</span>:</span>
SQLModel.metadata.create_all(engine)
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">create_heroes</span><span style="color: black;">()</span>:</span>
<span style="color: black;">with</span>Session(engine)<span style="color: black;">as</span> session:
team_preventers = Team(name=<span style="color: black;">"Preventers"</span>, headquarters=<span style="color: black;">"Sharp Tower"</span>)
team_z_force = Team(name=<span style="color: black;">"Z-Force"</span>, headquarters=<span style="color: black;">"Sister Margaret’s Bar"</span>)
hero_deadpond = Hero(
name=<span style="color: black;">"Deadpond"</span>,
secret_name=<span style="color: black;">"Dive Wilson"</span>,
teams=,
)
session.add(hero_deadpond)
session.commit()<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>
create_db_and_tables()
create_heroes()
<span style="color: black;">if</span> __name__ == <span style="color: black;">"__main__"</span>:
main()
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>fastapi-amis-admin</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">fastapi-amis-admin</span></strong><span style="color: black;"> 是一个<span style="color: black;">持有</span>高性能,<span style="color: black;">有效</span>率,易拓展的fastapi管理后台框架 启发自Django-Admin,并且<span style="color: black;">持有</span>不逊色于Django-Admin的强大功能.</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">官网文档 http://docs.amis.work/</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">界面预览</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">浏览器打开:</span><span style="color: black;"><http://127.0.0.1:8000/admin/></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/7e3ae26aa76e43d997cc4636bc5399c9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728742974&x-signature=VhfrPnrqLkPhspkvcvMf1bFCYdA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">image.png</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">amdin/doc预览</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">浏览器打开:</span><span style="color: black;">http://127.0.0.1:8000/admin/docs</span> :</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/b6cb7c3bf2984ff6a2ca1d4826d34be9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728742974&x-signature=50K3qXMiZVF2gHubkOlmi24PvIs%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">快速<span style="color: black;">运用</span> fastapi-amis-admin</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">安装</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">pip install fastapi_amis_admin</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">简单示例</h1>
<span style="color: black;">from</span> fastapi <span style="color: black;">import</span>FastAPI<span style="color: black;">from</span> fastapi_amis_admin.admin.settings <span style="color: black;">import</span> Settings
<span style="color: black;">from</span> fastapi_amis_admin.admin.site <span style="color: black;">import</span> AdminSite
<span style="color: black;"># 创建FastAPI应用 </span>
app = FastAPI()
<span style="color: black;"># 创建AdminSite实例 </span>site = AdminSite(settings=Settings(database_url_async=<span style="color: black;">sqlite+aiosqlite:///amisadmin.db</span>))
<span style="color: black;"># 挂载后台管理系统 site.mount_app(app) </span>
<span style="color: black;">if</span> __name__ == <span style="color: black;">__main__</span>:
<span style="color: black;">import</span>uvicorn
uvicorn.run(app, debug=<span style="color: black;">True</span>)
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">更加多</span>高级用法</h1>
seo常来的论坛,希望我的网站快点收录。 楼主发的这篇帖子,我觉得非常有道理。 外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢!
页:
[1]