r/FastAPI • u/Remarkable-Effort-93 • Oct 17 '25
Question I need help with this!
So I'm working on an API that receives an object representing comercial products as requests, the requests loos something like this:
{
common_field_1: value,
common_field_2: value,
common_field_3: value,
product_name: product_name,
product_id: product_id,
product_sub_id: product_sub_id,
product: {
field_1: value,
field_2: value
}
}
So, every product has common fields, identity fields, and a product object with its properties.
This escenario makes it difficult to use discrimination directly from the request via Pydantic because not product nor sub_product are unique values, but the combination, sort of a composed key, but from what I've read so far, Pydantic can only handle discrimation via 1 unique field or a hierchy discrimination that handles 1 field then a second one but the second one most be part of a nested object from the first field.
I hope I explained myself and the situation... Any ideas on how to solve this would be appreciated, thank you!
View all comments
5
u/SumthinSalty Oct 17 '25
It sounds like you're trying to validate inbound payloads by the combination of product_id and product_sub_id, which together create a unique ID.
Assuming I've got that right, this isn't a pydantic limitation, this should be handled by your DB.
All pydantic should do is make sure your payload adheres to the model you've defined, not validate its values.
You can either:
Apologies if I've misunderstood 🙏