Skip to main content

fastapi extend

Project description

1、install

pip install fastapi_extend

2、use

from sqlalchemy import Column, String, Integer, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base  # ORM(对象关系映射)的基类
Base = declarative_base()
from fastapi_extend.serializer import Serializer

class Trade(Base):
    __tablename__ = "trade"
    id = Column(Integer, primary_key=True)
    account = Column(String(20), default=None, nullable=False)
    trade_datetime = Column(DateTime)
    volume = Column(Float, default=None, nullable=False)
    price = Column(Float, default=0, nullable=False)
    order_id = Column(String(20))

class TradeSer(Serializer):
    account: str
    trade_datetime = Column(DateTime)
    volume: float
    price: float
    order_id: str

1)、Pagination

from fastapi_extend.pagenator import PageNumberPagination

from pydantic import validator
from sqlalchemy import Column, String, Integer, Float, DateTime

paginator = PageNumberPagination(
            params,
            Trade,
            TradeSer,
            exclude={"start", "end"},
        )
query = paginator.get_queryset()
if params.start:
    query = query.where(Trade.trade_datetime >= params.start)
if params.end:
    query = query.where(Trade.trade_datetime <= params.end)
data = await paginator.paginate_query(query, session)

2)、Serializer

from fastapi_extend.serializer import Serializer
from pydantic import validator

class TradeSer(Serializer):
    account: str
    volume: float
    price: float
    order_id: str

    @validator("price")
    def price_add(cls, v):
        return v + 10

data_ser = TradeSer.dump(result.scalars(), many=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fastapi_extend-0.1.3-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_extend-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_extend-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dd5d65d6dbc4c2e0c7439530a0593a0807244513f6f7bbbb41584a19745867be
MD5 12c39eb607d969e8222a8adc342553c1
BLAKE2b-256 3cccabe6bb9ba2b1e299a4fe7ef4446550f4ec373fd34a2143b0764cb6f30e04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page