Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17071

Pydantic union discriminator based on filed on parent model

$
0
0

i have model like this:

class Foo(BaseModel):    protocol: str    protocol_params: Union[ProtoclOneParam, ProtocolTwoParam]

ProtoclOneParam and ProtocolTwoParam have no same field with distinguishable value so I can use them as Discriminator and the only way that I can understand which model should be used for protocol_params is through the value of protocol which can either be "protocol1" or "protocol2".

if protocol == "protocol1" then protocol_params should be deserialized using the ProtoclOneParam model and so on.

in actual use cases, there are more than 5 protocols without any distinguishable field among them.

is there any way in pedantic to achieve what I need?


Viewing all articles
Browse latest Browse all 17071

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>