这是我的数据库架构。
我像这样定义了我的架构:
from pydantic import BaseModel
class Userattribute(BaseModel):
name: str
value: str
user_id: str
id: str
这是我的模型:
class Userattribute(Base):
__tablename__ = "user_attribute"
name = Column(String)
value = Column(String)
user_id = Column(String)
id = Column(String, primary_key=True, index=True)
在 crud.py 文件中,我定义了一个名为 get_attributes
的方法。
def get_attributes(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Userattribute).offset(skip).limit(limit).all()
这是我的GET
端点:
@app.get("/attributes/", response_model=List[schemas.Userattribute])
def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
users = crud.get_attributes(db, skip=skip, limit=limit)
print(users)
return users
连接到数据库似乎有效,但问题在于数据类型:
pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
response -> 0
value is not a valid dict (type=type_error.dict)
response -> 1
value is not a valid dict (type=type_error.dict)
response -> 2
value is not a valid dict (type=type_error.dict)
response -> 3
value is not a valid dict (type=type_error.dict)
response -> 4
value is not a valid dict (type=type_error.dict)
response -> 5
value is not a valid dict (type=type_error.dict)
response -> 6
value is not a valid dict (type=type_error.dict)
为什么FASTApi在这里期望一个字典?我真的不太理解,因为我甚至无法打印响应。我该如何解决这个问题?
class config:
中打错了单词。 - Chirag Purohitorm_mode
解决了它...谢谢。 - joeyagreco