admin管理员组文章数量:1193935
I have an api endpoint to let user submit a form, this form has some nested objects:
class FoundForm(Model):
contact_details: FoundContactDetails
...other fields...
The embedded model of FoundContactDetails
class FoundContactDetails(EmbeddedModel):
name: str
phone_number: str
email: str
address: Optional[DetailedAddress] = None
bank_account: Optional[str] = None
bank_name: Optional[str] = None
The DetailedAddress
class DetailedAddress(EmbeddedModel):
street: str
ward: str
district: str
city: str
country: str
I want to let the user submit the form with name, phone_number, email which are required. While the address field, they can either omit it or have to provide all address object's fields.
I trying to fix by using schemas for requests validation:
async def create_found_form(form_data: FoundFormCreate) -> FoundFormRead:
form = FoundForm(**form_data.model_dump())
return await engine.save(form)
The FoundFormBase:
class FoundFormBase(BaseModel):
contact_details: FoundContactDetails #the embedded model at the top of this post
FoundFormCreate for requests validation, extend from FoundFormBase above:
class FoundFormCreate(FoundFormBase):
@field_validator('contact_details')
def validate_contact_details(cls, value):
if value.bank_account is not None and value.bank_name is None:
raise ValueError('Bank name is required if bank account is provided')
if value.address is not None:
if not all([value.address.street, value.address.ward, value.address.district, value.address.city, value.address.country]):
raise ValueError('All address fields (street, ward, district, city, country) are required if address is provided')
return value
But when user submit the form with this json using SwaggerUI which is auto-generated by FastAPI:
{
"contact_details": {
"name": "string",
"phone_number": "string",
"email": "string",
"address": {
"street": "string",
"ward": "string",
"district": "string",
"city": "string",
"country": "string"
},
"bank_account": "string",
"bank_name": "string"
}
}
It's giving error, which believe to happens on address field.
"detail": "string indices must be integers, not 'str'"
Removing bank_account or both bank_account and bank_name is ok since the validation works properly.
Thank you for reading.
How could I handle multiple nested objects, especially has some required and optional mix-in, also let ODMantic/Pydantic access objects' fields properly.
I have an api endpoint to let user submit a form, this form has some nested objects:
class FoundForm(Model):
contact_details: FoundContactDetails
...other fields...
The embedded model of FoundContactDetails
class FoundContactDetails(EmbeddedModel):
name: str
phone_number: str
email: str
address: Optional[DetailedAddress] = None
bank_account: Optional[str] = None
bank_name: Optional[str] = None
The DetailedAddress
class DetailedAddress(EmbeddedModel):
street: str
ward: str
district: str
city: str
country: str
I want to let the user submit the form with name, phone_number, email which are required. While the address field, they can either omit it or have to provide all address object's fields.
I trying to fix by using schemas for requests validation:
async def create_found_form(form_data: FoundFormCreate) -> FoundFormRead:
form = FoundForm(**form_data.model_dump())
return await engine.save(form)
The FoundFormBase:
class FoundFormBase(BaseModel):
contact_details: FoundContactDetails #the embedded model at the top of this post
FoundFormCreate for requests validation, extend from FoundFormBase above:
class FoundFormCreate(FoundFormBase):
@field_validator('contact_details')
def validate_contact_details(cls, value):
if value.bank_account is not None and value.bank_name is None:
raise ValueError('Bank name is required if bank account is provided')
if value.address is not None:
if not all([value.address.street, value.address.ward, value.address.district, value.address.city, value.address.country]):
raise ValueError('All address fields (street, ward, district, city, country) are required if address is provided')
return value
But when user submit the form with this json using SwaggerUI which is auto-generated by FastAPI:
{
"contact_details": {
"name": "string",
"phone_number": "string",
"email": "string",
"address": {
"street": "string",
"ward": "string",
"district": "string",
"city": "string",
"country": "string"
},
"bank_account": "string",
"bank_name": "string"
}
}
It's giving error, which believe to happens on address field.
"detail": "string indices must be integers, not 'str'"
Removing bank_account or both bank_account and bank_name is ok since the validation works properly.
Thank you for reading.
How could I handle multiple nested objects, especially has some required and optional mix-in, also let ODMantic/Pydantic access objects' fields properly.
Share Improve this question asked Jan 24 at 8:42 Quang Truong NguyenQuang Truong Nguyen 1 01 Answer
Reset to default 0Solved: Replace EmbeddedModel by BaseModel
本文标签: pythonFastAPI ODMantic trying to access indices by 39str39Stack Overflow
版权声明:本文标题:python - FastAPI ODMantic trying to access indices by 'str' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738436085a2086685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论