从bean ODM文档中排除None字段



我试图从Beanie ODM插入一个文档,没有带None值的字段,但我找不到这样做的方法

@router.post('/signup')
async def signup(
request: Request,
user_create: SignupSchema
):
hashed_password = get_password_hash(user_create.password)
user_entity = UserEntity(**user_create.dict(), hashed_password=hashed_password)
result = await user_entity.insert()
if not result:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error creating user",
headers={"WWW-Authenticate": "Bearer"}
)
return JSONResponse(status_code=status.HTTP_201_CREATED,
content={"detail": "Account created successfully"})

类似user_create.dict(exclude_none=True),但与BeanieODM文档.insert(),我的UserEntity文档是这样的:

from typing import Optional
from beanie import Document
class UserEntity(Document):
username: str
email: EmailStr
first_name: Optional[str]
last_name: Optional[str]
hashed_password: str
class Settings:
name = "users"

我不希望字段first_name/last_name在数据库中,如果他们没有一个值。应该有一些方法使Beanie ODM文档的字段是可选的,对吗?

更新:现在支持使用设置中的keep_nulls标志。

https://beanie-odm.dev/tutorial/defining-a-document/keep-nulls

目前似乎不支持。

https://github.com/roman-right/beanie/discussions/322