mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-15 10:11:34 +08:00
📝 Update ModelRead to ModelPublic documentation and examples (#885)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@ -17,7 +17,7 @@ class HeroCreate(SQLModel):
|
||||
age: Optional[int] = None
|
||||
|
||||
|
||||
class HeroRead(SQLModel):
|
||||
class HeroPublic(SQLModel):
|
||||
id: int
|
||||
name: str
|
||||
secret_name: str
|
||||
@ -43,7 +43,7 @@ def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
@app.post("/heroes/", response_model=HeroPublic)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.model_validate(hero)
|
||||
@ -53,7 +53,7 @@ def create_hero(hero: HeroCreate):
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=List[HeroRead])
|
||||
@app.get("/heroes/", response_model=List[HeroPublic])
|
||||
def read_heroes():
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
|
@ -15,7 +15,7 @@ class HeroCreate(SQLModel):
|
||||
age: int | None = None
|
||||
|
||||
|
||||
class HeroRead(SQLModel):
|
||||
class HeroPublic(SQLModel):
|
||||
id: int
|
||||
name: str
|
||||
secret_name: str
|
||||
@ -41,7 +41,7 @@ def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
@app.post("/heroes/", response_model=HeroPublic)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.model_validate(hero)
|
||||
@ -51,7 +51,7 @@ def create_hero(hero: HeroCreate):
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=list[HeroRead])
|
||||
@app.get("/heroes/", response_model=list[HeroPublic])
|
||||
def read_heroes():
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
|
@ -17,7 +17,7 @@ class HeroCreate(SQLModel):
|
||||
age: Optional[int] = None
|
||||
|
||||
|
||||
class HeroRead(SQLModel):
|
||||
class HeroPublic(SQLModel):
|
||||
id: int
|
||||
name: str
|
||||
secret_name: str
|
||||
@ -43,7 +43,7 @@ def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
@app.post("/heroes/", response_model=HeroPublic)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.model_validate(hero)
|
||||
@ -53,7 +53,7 @@ def create_hero(hero: HeroCreate):
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=list[HeroRead])
|
||||
@app.get("/heroes/", response_model=list[HeroPublic])
|
||||
def read_heroes():
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
|
@ -18,7 +18,7 @@ class HeroCreate(HeroBase):
|
||||
pass
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
class HeroPublic(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
@ -41,7 +41,7 @@ def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
@app.post("/heroes/", response_model=HeroPublic)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.model_validate(hero)
|
||||
@ -51,7 +51,7 @@ def create_hero(hero: HeroCreate):
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=List[HeroRead])
|
||||
@app.get("/heroes/", response_model=List[HeroPublic])
|
||||
def read_heroes():
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
|
@ -16,7 +16,7 @@ class HeroCreate(HeroBase):
|
||||
pass
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
class HeroPublic(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
@ -39,7 +39,7 @@ def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
@app.post("/heroes/", response_model=HeroPublic)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.model_validate(hero)
|
||||
@ -49,7 +49,7 @@ def create_hero(hero: HeroCreate):
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=list[HeroRead])
|
||||
@app.get("/heroes/", response_model=list[HeroPublic])
|
||||
def read_heroes():
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
|
@ -18,7 +18,7 @@ class HeroCreate(HeroBase):
|
||||
pass
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
class HeroPublic(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
@ -41,7 +41,7 @@ def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
@app.post("/heroes/", response_model=HeroPublic)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.model_validate(hero)
|
||||
@ -51,7 +51,7 @@ def create_hero(hero: HeroCreate):
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=list[HeroRead])
|
||||
@app.get("/heroes/", response_model=list[HeroPublic])
|
||||
def read_heroes():
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero)).all()
|
||||
|
Reference in New Issue
Block a user