33模型管理在后台管理中是最为常用的一个管理类,其使用功能也是最为丰富的. fastapi-amis-admin目前已经实现针对数据模型常用的各种基本操作,
44并且你仍然可以在此基础上做出更多个性化的拓展.
55
6- ## 示例-1
6+ ## 示例1(基于SQLAlchemy 2.0)
7+ ``` python
8+ from sqlalchemy import String
9+ from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
10+ from typing import Optional
11+
12+
13+ class Base (DeclarativeBase ):
14+ pass
15+
16+
17+ class CategorySchema (BaseModel ):
18+ id : Optional[int ] = Field(default = None , primary_key = True , nullable = False )
19+ name: str = Field(title = " CategoryName" )
20+ description: str = Field(default = " " , title = " CategoryDescription" )
21+
22+ class Config :
23+ # orm_mode = True
24+ from_attributes = True
25+
26+
27+ # 创建SQLAlchemy 2.0模型,详细请参考: https://docs.sqlalchemy.org/en/20/orm/quickstart.html
28+ class Category (Base ):
29+ __tablename__ = " category"
30+ __pydantic_model__ = CategorySchema # 指定模型对应的Schema类.省略可自动生成,但是建议指定.
31+
32+ id : Mapped[int ] = mapped_column(primary_key = True , nullable = False )
33+ name: Mapped[str ] = mapped_column(String(100 ), unique = True , index = True , nullable = False )
34+ description: Mapped[str ] = mapped_column(String(255 ), default = " " )
35+ ```
36+ 在` app = FastAPI() ` 下方添加以下内容来创建数据表:
37+ ``` python
38+ @app.on_event (" startup" )
39+ async def startup ():
40+ await site.db.async_run_sync(Category.metadata.create_all, is_session = False )
41+ pass
42+ ```
43+
44+ ## ~~ 示例-1(基于SQLModel)~~
745
846``` python
47+
948# 先创建一个SQLModel模型,详细请参考: https://sqlmodel.tiangolo.com/
1049class Category (SQLModel , table = True ):
1150 id : Optional[int ] = Field(default = None , primary_key = True , nullable = False )
@@ -30,10 +69,42 @@ class CategoryAdmin(admin.ModelAdmin):
3069!!! note annotate "关于SQLModel模型"
3170
3271 事实上这部分代码并不属于`amis-admin`的代码,因为它可以用重用在任何需要ORM映射的地方, 在项目中你应该单独定义一个`models.py`文件编写这部分代码.
33-
72+
3473 SQLModel是一个非常优秀的Python ORM库,由FastAPI同一位作者编写,完美的结合了SQLAlchemy和Pydantic.请阅读它的官方文档: https://sqlmodel.tiangolo.com/
3574
36- ## 示例-2
75+ ## 示例-2(基于SQLAlchemy 2.0)
76+ ``` python
77+ from sqlalchemy import String, Integer, Boolean, ForeignKey, Select
78+ from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
79+ from typing import Optional
80+
81+ class Article (Base ):
82+ __tablename__ = ' article'
83+
84+ id : Mapped[int ] = mapped_column(Integer, primary_key = True , autoincrement = True )
85+ title: Mapped[str ] = mapped_column(String(200 ), nullable = False )
86+ description: Mapped[str ] = mapped_column(String(400 ), default = ' ' )
87+ status: Mapped[bool ] = mapped_column(Boolean, default = False )
88+ content: Mapped[str ] = mapped_column(String, nullable = False )
89+ category_id: Mapped[Optional[int ]] = mapped_column(Integer, ForeignKey(' category.id' ))
90+
91+
92+ @site.register_admin
93+ class ArticleAdmin (admin .ModelAdmin ):
94+ page_schema = ' 文章管理'
95+ model = Article
96+ # 设置需要展示的字段
97+ list_display = [Article.id, Article.title, Article.description, Article.status, Category.name]
98+ # 设置模糊搜索字段
99+ search_fields = [Article.title, Category.name]
100+
101+ # 自定义基础选择器
102+ async def get_select (self , request : Request) -> Select:
103+ stmt = await super ().get_select(request)
104+ return stmt.outerjoin(Category, Article.category_id == Category.id)
105+ ```
106+ 同时,将前文定义的` def startup() ` 函数追加一行` await site.db.async_run_sync(Article.metadata.create_all, is_session=False) ` 用于生成对应数据表。
107+ ## ~~~示例-2(基于SQLModel)~~~
37108
38109``` python
39110# 创建一个SQLModel模型,详细请参考: https://sqlmodel.tiangolo.com/
@@ -140,6 +211,6 @@ class ArticleAdmin(admin.ModelAdmin):
140211!!! note annotate "关于fastapi_amis_admin与django-admin"
141212
142213 [django-admin](https://docs.djangoproject.com/zh-hans/4.0/ref/contrib/admin/)是一个非常成熟强大的web管理后台工具,使用django的用户应该经常使用到它,但他是并不适用于非django项目,这也是fastapi_amis_admin诞生的主要原因之一.
143-
214+
144215 fastapi_amis_admin相比django-admin拥有更多的拓展与功能,但是fastapi_amis_admin目前仍然处于成长阶段,很多功能并不成熟,需要漫长的不断完善与升级.非常期待你参与到[fastapi_amis_admin](https://github.com/amisadmin/fastapi_amis_admin)的项目开发之中,为项目贡献代码,或为项目[提供建议](https://github.com/amisadmin/fastapi_amis_admin/issues).
145216
0 commit comments