gpt4 book ai didi

python - FastAPI 给出 ​​ "value is not a valid dict (type=type_error.dict)"

转载 作者:行者123 更新时间:2023-12-05 01:23:40 24 4
gpt4 key购买 nike

我在这个项目上学习 FastAPI 并遇到了这个问题——任何请求,无论是 GET 还是 POST 都会给我一个错误:value is not a valid dict (type=type_error.dict)。我的代码有什么问题。我的模型.py

class User(Base):
__tablename__ = "users"

id = Column(Integer, primary_key=True, index=True)
telegram_id = Column(Integer, unique=True, index=True)
username = Column(String(50))
pet_name = Column(String(50))
language_code = Column(String(5))

sent_items = relationship("Log", back_populates="recipient")

class Log(Base):
__tablename__ = "sent_log"

id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
recipient = relationship("User", back_populates="sent_items")

article_id = Column(Integer, ForeignKey("articles.id"))
article = relationship("Article", back_populates="items")

class Article(Base):
__tablename__ = "articles"

id = Column(Integer, primary_key=True, index=True)
text = Column(String(1024))
image_url = Column(String(500), index=True)
language_code = Column(String(255))

items = relationship("Log", back_populates="article")

模式:

class ArticleBase(BaseModel):
text: str
image_url: str
language_code: str

class ArticleCreate(ArticleBase):
pass

class ArticleEdit(ArticleBase):
id: int

class Config:
orm_mode = True

class UserBase(BaseModel):
telegram_id: int
username: str
pet_name: str
language_code: str

class UserCreate(UserBase):
pass

class UserEdit(UserBase):
id: int

class Config:
orm_mode = True

crud.py

def get_articles(db: Session, skip: int = 0, limit: int = 100):
"""Get all articles."""
return db.query(models.Article).offset(skip).limit(limit).all()


def get_article(db: Session, article_id: int):
"""Get article by id."""
return db.query(models.Article).filter(models.Article.id == article_id).first()


def create_article(db: Session, article: schemas.ArticleCreate):
db_article = models.Article(
text=article.text,
image_url=article.image_url,
language_code=article.language_code
)
db.add(db_article)
db.commit()
db.refresh(db_article)
return db_article

主要.py

@app.get("/users/{user_telegram_id}", response_model=schemas.UserBase, tags=["user"])
def read_user(user_telegram_id: int, db: Session = Depends(get_db)):
db_user = crud.get_user(db, user_telegram_id=user_telegram_id)
if db_user is None:
raise HTTPException(status_code=404, detail="User not found")
return db_user


@app.post("/articles/", response_model=schemas.ArticleCreate, tags=["article"])
def create_article(article: schemas.ArticleCreate, db: Session = Depends(get_db)):
return crud.create_article(db=db, article=article)


@app.get("/articles/", response_model=schemas.ArticleBase, tags=["article"])
def read_articles(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
items = crud.get_articles(db, skip=skip, limit=limit)
return items


@app.get("/articles/{article_id}", response_model=schemas.ArticleBase, tags=["article"])
def read_article(article_id: int, db: Session = Depends(get_db)):
db_article = crud.get_article(db, article_id=article_id)
if db_article is None:
raise HTTPException(status_code=404, detail="Article not found")
return db_article

顺便说一下,@app.post("/articles/"... 抛出一个错误,但也在数据库中插入了一条数据。

最佳答案

问题出在你的返回上,这就是数据库中插入工作的原因。您想要返回 ArticleCreate 架构 (response_model=schemas.ArticleCreate),除了您直接返回数据库响应而没有任何格式。因此,您必须将 crud 响应转换为模式响应。

@app.post("/articles/", response_model=schemas.ArticleCreate, tags=["article"])
def create_article(article: schemas.ArticleCreate, db: Session = Depends(get_db)):
response = crud.create_article(db=db, article=article)
return schemas.ArticleCreate(**response.dict())

或者简单地在你的ArticleCreate类中使用orm模式

关于python - FastAPI 给出 ​​ "value is not a valid dict (type=type_error.dict)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72074928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com