title: Can I or How To automatically identify the pydantic schema based on a couple of key attribute value?
body: <p>I am playing around with pydantic, and what I'm trying to do is something like this.</p>
  <p>I have a <code>BaseSchema</code> which contains two &quot;identifier&quot; attributes, say <code>first_identifier_attribute</code> and <code>second_identifier_attribute</code>.</p>
  <p>And I have two other schemas that inherit the <code>BaseSchema</code>.</p>
  <p>I'd like for pydantic to automatically cast my dictionary into one of the two schemas based on the value of the &quot;identifier&quot; attributes.</p>
  <p>For example, in the following case, casting <code>some_dict</code> to <code>UnifiedSchema</code> with the &quot;identifier&quot; attributes values defined, should cast to <code>SecondSchema</code>. Currently, this does not work as expected because even though I provide the &quot;identifier&quot; attribute values of <code>SecondSchema</code>, the <code>UnifiedSchema.Data</code> shows <code>FirstSchema</code>.</p>
  <p>Is this something doable even?</p>
  <pre><code>from pydantic import BaseModel
  from typing import Union
  
  
  class BaseSchema(BaseModel):
  
      first_identifier_attribute: str
      second_identifier_attribute: str
  
  
  class FirstSchema(BaseSchema):
  
      first_identifier_attribute = 'A'
      second_identifier_attribute = '1'
      first_schema_attribute: str
  
  
  class SecondSchema(BaseSchema):
  
      first_identifier_attribute = 'B'
      second_identifier_attribute = '2'
      second_schema_attribute: str
  
  
  AllSchemaUnion = Union[FirstSchema, SecondSchema]
  
  
  class UnifiedSchema(BaseSchema):
      class Config:
          arbitrary_types_allowed = True
  
      Data: AllSchemaUnion
  
  
  some_dict = {
      'second_schema_attribute': 'b val',
      'first_schema_attribute': 'a val',
  }
  
  result = UnifiedSchema(
      first_identifier_attribute='B', second_identifier_attribute='2', Data={**some_dict}
  )
  print(result)
  </code></pre>