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.1.0.tar.gz
.
File metadata
- Download URL: fastapi_sqlmodel_crud-0.1.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55fecad698d903ad50cef7fd72e90f9066f0e1dd672f48ba32a5a950cd165779 |
|
MD5 | be9c535846be56691c1e2022303455f9 |
|
BLAKE2b-256 | 1f23c6d9fe4138ba853568172871d8afc4f517994875a6204f1719f755d6b77c |
File details
Details for the file fastapi_sqlmodel_crud-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_sqlmodel_crud-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70d3c5429752e8a64663d5cdef2a0d81e0b6c2d6dd3f2f2edc562953c7929bae |
|
MD5 | 88ab73a428bc863290ad018d38ba0db0 |
|
BLAKE2b-256 | 88920c3894c9ad2bb36e2724b9ad323219e4c32b2676b17622ac33d9d186488a |