- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这个项目上学习 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/
我收到了一些奇怪的 HTTP 请求,例如: "POST /type_error:SafeUrl HTTP/1.1" "GET /some/path/type_error:SafeUrl HTTP/1.
FastAPI 新手 获取“值不是有效列表 (type=type_error.list)”错误 每当我尝试返回 {"posts": post} @router.get('', response_mod
我在这个项目上学习 FastAPI 并遇到了这个问题——任何请求,无论是 GET 还是 POST 都会给我一个错误:value is not a valid dict (type=type_error
我是一名优秀的程序员,十分优秀!