fastapi-sqlmodel-crud is a program which is based on fastapi+sqlmodel and used to quickly build the Create, Read, Update, Delete generic API interface.
Project description
项目介绍
fastapi-sqlmodel-crud
是一个基于FastAPI
+SQLModel
, 用于快速构建Create,Read,Update,Delete通用API接口的项目.
安装
pip install fastapi-sqlmodel-crud
简单示例
main.py:
from typing import Optional, Generator, Any
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel, Field
from sqlmodel.ext.asyncio.session import AsyncSession
from fastapi_amis_admin.crud import SQLModelCrud
# 1.创建SQLModel模型
class Article(SQLModel, table=True):
id: int = Field(default=None, primary_key=True, nullable=False)
title: str = Field(title='ArticleTitle', max_length=200)
description: Optional[str] = Field(default='', title='ArticleDescription', max_length=400)
status: bool = Field(None, title='status')
content: str = Field(title='ArticleContent')
# 2.创建 AsyncSession
database_url = 'sqlite+aiosqlite:///admisadmin.db'
engine: AsyncEngine = create_async_engine(database_url, future=True, pool_recycle=1200)
session_maker: sessionmaker = sessionmaker(engine, class_=AsyncSession,
expire_on_commit=False, autocommit=False, autoflush=False)
async def session_factory() -> Generator[AsyncSession, Any, None]:
async with session_maker() as session:
yield session
# 3. 注册crud路由
article_crud = SQLModelCrud(model=Article, session_factory=session_factory).register_crud()
app = FastAPI(debug=True)
# 4. 包含路由器
app.include_router(article_crud.router)
# 5. 创建模型数据库表(首次运行)
@app.on_event("startup")
async def startup():
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
开发文档
依赖项目
许可协议
该项目遵循 Apache2.0 许可协议。
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
Built Distribution
File details
Details for the file fastapi_sqlmodel_crud-0.0.15.tar.gz
.
File metadata
- Download URL: fastapi_sqlmodel_crud-0.0.15.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e457b0303d59382f4d06008e2acd2f7bbd21481a13f12b687a938615be612ba7 |
|
MD5 | c911d309f13de962149dd3fe55623f97 |
|
BLAKE2b-256 | 3d83874bd6c4a053e6a6395a6ae1bd2768bf580c06976185687cde70dbc324f0 |
File details
Details for the file fastapi_sqlmodel_crud-0.0.15-py3-none-any.whl
.
File metadata
- Download URL: fastapi_sqlmodel_crud-0.0.15-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 646abb3d2e72e30924333fdc62fb85d7690793c1a875790d5e2b66a28e336cc8 |
|
MD5 | 18744b1ca34d26dd7e50cbaeeab6c9e8 |
|
BLAKE2b-256 | dc001e8380d9d7a410f71470db0549a5a51fee4f2b95b0e392c43aa3bc46de49 |