mongoengine.errors.FieldDoesNotExist:文档"{'username', 'password'}"上不存在的字段"Users":FastAPI 中的错误



我正在学习FastAPI并使用MongoDB。我正试图在User对象上发出发布请求,但有一些错误我无法解决。代码如下:用户类

class Users(Document):
username: StringField()
password: StringField()
class NewUsers(BaseModel):
username: str
password: str

我正在尝试将用户保存为:-

@app.post('/signup')
def sign_up(new_user: NewUsers):
user = Users(
username=new_user.username,
password=new_user.password
)
user.save()
return {"message": "New user is successfully created!"}

然后它抛出这样的错误:

mongoengine.errors.FieldDoesNotExist:字段"{'用户名','密码'}"文件上不存在";用户";

MongoEngine不支持开箱即用的类型提示,需要使用"当设置字段时,不要":">

class Users(Document):
username = StringField()
password = StringField()

最新更新