我对pydantic和fastapi有点陌生,但在请求获取所有用户时,我得到了正常的响应,但在尝试创建新用户时开始出现此错误。它是一个API,由python 3.11.4上的fastapi和sqlalchemy库组成
pydantic.error_wrappers.ValidationError: 4 validation errors for User
response -> username
field required (type=value_error.missing)
response -> email
field required (type=value_error.missing)
response -> access
field required (type=value_error.missing)
response -> id
field required (type=value_error.missing)
和用户在我的schemas.py文件中分类如下:
import pydantic as _pydantic
from pydantic import BaseModel as _BaseModel
from datetime import datetime
from typing import List, Optional
#USERS
class _UserBase(_pydantic.BaseModel):
username: str
email: str
access: str
class UserCreate(_UserBase):
username: str
email: str
password: str
access: str
class Config:
orm_mode=True
class User(_UserBase):
id:int
projects: List["Project"] = []
class Config:
orm_mode=True
型号.py
class User(_database.Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, index=True)
email = Column(String, unique=True, index=True)
password = Column(String)
access = Column(String)
projects = _orm.relationship("Project", back_populates="owner")
tasks = _orm.relationship("Task", back_populates="assigned_user")
def verify_password(self, password:str):
return _hash.bcrypt.verify(password, self.password)
和在役.py
async def create_user(db: _orm.Session, user: _schemas.UserCreate):
db_user = _models.User(email=user.email, password=_hash.bcrypt.hash(user.password), username=user.username, access=user.access)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
有人能解释一下为什么会发生这种错误吗。非常感谢。
尝试在Schemas.py中的_ProjectBase、ProjectCreate和Project之间更改字段的位置,但错误并没有消失