网站小图标怎么做的,网址导航建站,湛江网站制作企业,做网站推广用优化还是竞价之前我们创建的文件都是在一个目录中#xff0c;但是在我们的实际开发中#xff0c;肯定不能这样设计#xff0c;那么我们去创建一个目录#xff0c;叫models#xff0c;大致如下。 主要目录是#xff1a; __init__.py 是一个空文件#xff0c;说明models是一个package…之前我们创建的文件都是在一个目录中但是在我们的实际开发中肯定不能这样设计那么我们去创建一个目录叫models大致如下。 主要目录是 · __init__.py 是一个空文件说明models是一个package
· crud.py 数据库操作相关
· database.py 数据库配置相关
· models.py 数据库模型表
· schemas.py 模型验证
· main.py 主文件
database.py代码如下
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmakerconn mysqlpymysql://{username}:{password}{host}:{port}/{database}?charsetutf8.format(usernameroot, password123456, host10.30.10.36, port3306, databasefastapi_learn_road)
engine create_engine(conn)# 该类的每个实例都是一个数据库会话该类本身还不是数据库会话但是一旦我们创建了SessionLocal的实例这个实例将是实际的数据库会话
SessionLocal sessionmaker(autocommitFalse, autoflushFalse, bindengine)# 创建数据库基类
Base declarative_base()models.py代码如下
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from .database import Baseclass User(Base):__tablename__ usersid Column(Integer, primary_keyTrue, indexTrue)email Column(String(10), uniqueTrue, indexTrue)hashed_password Column(String(100))is_active Column(Boolean, defaultTrue)items relationship(Item, back_populatesowner)class Item(Base):__tablename__ itemsid Column(Integer, primary_keyTrue, indexTrue)title Column(String(10), indexTrue)description Column(String(10), indexTrue)owner_id Column(Integer, ForeignKey(users.id))owner relationship(User, back_populatesitems)schemas.py代码如下定义请求参数模型验证与响应模型验证的Pydantic模型。
from pydantic import BaseModel
from typing import List, Optionalclass BaseItem(BaseModel):title: strdescription: Optional[str] Noneclass ItemModel(BaseItem):passclass ItemOut(BaseItem):id: intowner_id: intclass Config:orm_mode Trueclass BaseUser(BaseModel):email: strclass UserModel(BaseUser):请求参数模型password: strclass UserOut(BaseUser):响应模型id: intis_active: boolitems: List[ItemOut]class Comfig:orm_mode Truecrud.py代码如下
# 之前都是把所有逻辑写到了接口函数里其实我们应该抽出来一起管理
from fastapi import HTTPException
from sqlalchemy.orm import Session
from .models import *
from .schemas import *def get_user_method(db: Session, uid: int):user db.query(User).filter(User.id uid).first()if not user:raise HTTPException(status_code404, detailuser not exists)return userdef create_user_method(db: Session, user: UserModel):db_user db.query(User).filter(User.email user.email).first()if db_user:raise HTTPException(status_code200, detailthis user already exists)fake_hashed_password user.password _hashedinit_user User(emailuser.email, hashed_passwordfake_hashed_password)db.add(init_user)db.commit()db.refresh(init_user)return init_userdef get_items_method(db: Session, skip: int 0, limit: int 10):return db.query(Item).offset(skip).limit(limit).all()def get_items_by_uid_method(db: Session, uid: int):user db.query(User).filter(User.id uid).first()if not user:raise HTTPException(status_code200, detailthis user is not valid)return db.query(Item).filter(Item.owner user).offset(0).limit(2).all()def create_item_by_user_method(db: Session, uid: int, item: ItemModel):init_item Item(**item.dict(), owner_iduid)db.add(init_item)db.commit()db.refresh(init_item)return init_itemmain.py代码如下
from fastapi import FastAPI, Depends, HTTPException
from models.crud import *
from models.database import *app FastAPI()def create_db():每个请求处理完毕后关闭当前连接不同的请求使用不同的链接db SessionLocal()try:yield dbfinally:db.close()app.post(/user, response_modelUserOut)
def create_user(user: UserModel, db: Session Depends(create_db)):return create_user_method(db, user)app.get(/user, response_modelUserOut)
def get_user(uid: int, db: Session Depends(create_db)):return get_user_method(db, uid)app.post(/items/{uid}, response_modelItemOut)
def create_item_by_user(uid: int, item: ItemModel, db: Session Depends(create_db)):return create_item_by_user_method(db, uid, item)app.get(/items, response_modelList[ItemOut])
def get_items(skip: int 0, limit: int 10, db: Session Depends(create_db)):return get_items_method(db, skip, limit)app.get(/items/{uid}, response_modelList[ItemOut])
def get_items_by_uid(uid: int, db: Session Depends(create_db)):return get_items_by_uid_method(db, uid)if __name__ __main__:import uvicornuvicorn.run(main:app, reloadTrue)我们目前是这么改造的。后续还会持续改造的。目前我们没有对API接口main文件进行改造下面的分享我们会对api接口做改造。 文章转载自: http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.qymqh.cn.gov.cn.qymqh.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.kaakyy.com.gov.cn.kaakyy.com http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.bpwz.cn.gov.cn.bpwz.cn http://www.morning.cfhwn.cn.gov.cn.cfhwn.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn