A paginator based on fastapi and tortoise-orm
Project description
这个是一个基于FastApi+tortoise-orm的查询集分页器
主要通过对QuerySet进行分页排序等进行操作,再对即将响应的数据进行序列化,相比较一些先序列化再分页的分页器,这个会更轻简快速。
一、介绍 该分页器主要分为三个块:Params、PagePydantic和pagination
- Params 前端传的参数,包括page(页数)、size(页面大小)和order_by(排序字段,选传)
- PagePydantic
total: int # 查询总数
page: int
size: int
total_pages: int # 总页数
next: str # 下页url
previous: str # 上页url
响应模型,即返回给前端的数据模型 2. pagination 分页操作,将查询集QuerySet进行分页或者排序等操作,然后在根据用户自定义的Pydantic进行序列化,最后返回分页后的数据
二、快速开始:
- install(安装)
pip install fastapi-tortoise-pagination
- 使用
class ForumArticle(models.Model):
"""论坛文章"""
id = fields.IntField(pk=True)
user = fields.ForeignKeyField("cp_model.User", on_delete=fields.CASCADE)
title = fields.CharField(max_length=32, default='', null=True) # 文章标题
content = fields.TextField(default='', null=True, blank=True) # 文章正文
class ForumArticleSchema(
pydantic_model_creator(ForumArticle,
name="ForumArticleSchema",
exclude=())
):
pass
class TestViewSet(BaseViewSet):
model = ForumArticle
schema = ForumArticleSchema
pk_type = str
views = {
}
@Action.get("/list", response_model=PagePydantic[ForumArticleSchema],description="分页获取帖子列表") #
async def list(self, params: Params = Depends()):
query_set = ForumArticle.filter(is_delete=0)
return await pagination(pydantic_model=ForumArticleSchema, query_set=query_set, params=params)
Project details
Release history Release notifications | RSS feed
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_tortoise_pagination-0.1.3.tar.gz
.
File metadata
- Download URL: fastapi_tortoise_pagination-0.1.3.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 540d5a3651d6a6ba6b0d931267c0ca82e2f0ab14b0f5ffb94f80459b26cbba21 |
|
MD5 | 364c2b1a99029ce3160cfcdb3f85d8ca |
|
BLAKE2b-256 | 590ba6de0ce3eec614f5d01810955624b831cafc6ca0e937de3c940579e2f3e5 |
File details
Details for the file fastapi_tortoise_pagination-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: fastapi_tortoise_pagination-0.1.3-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c21c271a8669dcdc4705fee424b862033839fb47eeaadb670eb561f80479a76f |
|
MD5 | 12e13e19a4dafd5bd15a3b901078b9fe |
|
BLAKE2b-256 | 4d41f9904f2030affd47d1908603a26d4e427214054f516996cb04dd61c83969 |