Package for creating endpoint
Project description
ViewSets for FastApi
Installation
For install package run command
pip install fastapi_viewsets
Usage
Create main.py
and copy this code:
from typing import Optional
from fastapi import FastAPI
from fastapi_viewsets import Base as BaseViewset
from fastapi_viewsets.db_conf import Base, get_session, engine
from pydantic import BaseModel
from sqlalchemy import Column, Integer, String, Boolean
# create fastapi app
app = FastAPI()
class UserSchema(BaseModel):
"""Pydantic Schema"""
id: Optional[int]
username: str
password: str
is_admin: Optional[bool]
class Config:
orm_mode = True
class User(Base):
"""SQLAlchemy model"""
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True)
password = Column(String(255))
is_admin = Column(Boolean, default=False)
Base.metadata.create_all(engine) # create table in db (by default this is sqlite3 base.db)
# create viewset for our model
user_model = BaseViewset(prefix='/users',
endpoint='/user',
model=User,
response_model=UserSchema,
db_session=get_session
)
# register all methods GET POST PUT PATCH DELETE
user_model.register()
# add rote to fastapi
app.include_router(user_model)
Run by command uvicorn main:app --reload
After start application go to docs
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
Close
Hashes for fastapi_viewsets-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e2aae2988744680ddc80ac125c4d6a906e3df803d5a2a7a4e09bac1e68af6ba |
|
MD5 | 72de8b6a32be88df278c8843e9bc395a |
|
BLAKE2b-256 | 8386a38d53c9ad21635bf106827afc3a68f5239e226a81bb63368c998b24e093 |