FastAPI – TodoList 作り

with FastAPI I made a server with local first. The FastAPI tutorial shows about User and Item, so the code can be transformed and made wellSQL (relational) Database – FastAPISQL (relational) database ¶ Warning There is no translation of this language on the current page. But I can help you translate: To contribute. FastAPI does not require the use of SQL (relational) databases. However, you can use the relationship database that you want. So let’s look at an example here u…fastapi.tiangolo.comThe overall folder structure that we finally created is as follows.I don’t know if this is a standard or not, but I’ll write it like this (_init__, py is a file that tells you it’s a package)First, I made a todolist using sqlite instead of mysql.Set up from the virtual environment in pipenv (I was using 3.10.5, but the version has already been upgraded)pipenv –python 3.10.5 pipenv シェル # 가상환경 들어가기 pipenv install fastapi[all] pipenv install SQLlchemyIt’s still incomplete, but I’ll raise the code just in case. I plan to post the corrections on GitHub later. I will look at the app/main.pyfrom fastapi import FastAPIapp = FastAPI() # 인스턴스 생성 from app.api.api_v1.api import api_routerapp = FastAPI()app.include_router(api_router) # api_router와 연결앱/ API_v1/api.pyfrom fastapi import APIRouter from app.api.api_v1.endpoints import to dosapi_router = APIRouter()# to do 로 들어오는 api_router.include_router (todos.router, prefix=’/todos’, tags=[‘todo’])app/api/api_v1/endpoints/todos.py ですapp/api/api_v1/endpoints/todos.py ですApp/crud/todos.pyApp/crud/todos.pyapp/db/base.pyfrom sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm インポートセッションメーカーSQから取得しますLALCHEMY_DATABASE_URL = “sqlite:///.app.db” engine = create_engine (SQLCHEMY_DATABASE_URL, connect_args = {“check_same_thread”: False}) ですSessionLocal = sessionmaker(autocommit= false、autoflush= false、bind=Engine) is the Base= debug d debugsapp/models/todos.py ですand then click the .sqlalchemy import Boolean, Column, ForeignKey, Integer, String, Date from app.db.base import ベースクラス Todos(Base):__tablename__ = ‘todos’ id = Column(Integer, primary_key=True, index= True) date = Column(Date) content = Column(Strumn(String) is_done) done= ColApps/Shumas/todos.pyfrom datetime import from pydanticimportBaseModel クラス TodosBase(BaseModel):date:日付内容:stris_done:boolclass TodoCreate(TodosBase):passclass Todo(TodosBase):id:int クラス設定:orm_mode =True ですWe have verified that this level of implementation also works.If you enter localhost/docs, you can do apitest.If you want to see the contents of the DB, install the following program and use the app.You can open the db file.DB Browser for SQLite DB Browser for SQLite DB Browser for SQLite Official Home SQLite Screenshot DB Browser for SQLite (DB4S) is a high-quality, visual, open-source tool for creating, designing, and editing SQLite-compatible database files. DB4S is for users and developers who want to create, search and edit…sqlitebrowser.orgDB Browser for SQLite DB Browser for SQLite DB Browser for SQLite Official Home SQLite Screenshot DB Browser for SQLite (DB4S) is a high-quality, visual, open-source tool for creating, designing, and editing SQLite-compatible database files. DB4S is for users and developers who want to create, search and edit…sqlitebrowser.orgDB Browser for SQLite DB Browser for SQLite DB Browser for SQLite Official Home SQLite Screenshot DB Browser for SQLite (DB4S) is a high-quality, visual, open-source tool for creating, designing, and editing SQLite-compatible database files. DB4S is for users and developers who want to create, search and edit…sqlitebrowser.org

error: Content is protected !!