English 中文(简体)
pydantic.error_wrappers.ValidationError:用户类型=value_error缺少4个验证错误
原标题:pydantic.error_wrappers.ValidationError: 4 validation errors for User type=value_error.missing

我对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之间更改字段的位置,但错误并没有消失

问题回答

在UserCreate类中,u继承自UserBase,因此不必再次定义相同的属性。

第二件事是,在services.py中,您将响应模型定义为UserCreate,它并没有您试图返回的id atribiute。您应该返回用户:

async def create_user(db: _orm.Session, user: _schemas.User):

这应该行得通。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签