title: Pydantic: How to use one field's value to set values for other fields?
body: <h3><strong>What I Have</strong></h3>
  <p><strong>Dictionary:</strong></p>
  <pre><code>user_dict = {'user': {'field1': 'value1',
                        'field2': 'value2'},
               'admin':{'field1': 'value3',
                        'field2': 'value4'}}
  </code></pre>
  <p><strong>Pydantic Model:</strong></p>
  <pre><code>class User(BaseModel):
      account_type: Optional[str] = 'user'
      field1: Optional[str] = ''
      field1: Optional[str] = ''
  
      class Config:
          validate_assignment = True
  
      @validator(&quot;account_type&quot;, pre=True, always=True)
          def _set_account_type(cls, account_type: str):
          return account_type or &quot;user&quot;
  </code></pre>
  <h3><strong>Desired Outcome</strong></h3>
  <p>I would like to lookup the values of field1 and field2 from the user_dict based upon the account_type. So I should be able to do something like this:</p>
  <pre><code>user = User()
  print(user.field1)
  # &gt; 'value1'
  
  print(user.field2)
  # &gt; 'value2'
  
  user2 = User(field1=None, field2=None)
  print(user2.field1)
  # &gt; 'value1'
  
  print(user2.field2)
  # &gt; 'value2'
  
  user3 = User(account_type=&quot;admin&quot;)
  print(user3.field1)
  # &gt; 'value3'
  
  print(user3.field2)
  # &gt; 'value4'
  </code></pre>
  <h3><strong>What I have tried</strong></h3>
  <pre><code>class User(BaseModel):
      account_type: Optional[str] = 'user'
      field1: Optional[str] = user_dict['account_type']['field1']
      field1: Optional[str] = user_dict['account_type']['field2']
  
      class Config:
          validate_assignment = True
  
      @validator(&quot;account_type&quot;, pre=True, always=True)
          def _set_account_type(cls, account_type: str):
          return account_type or &quot;user&quot;
  
  user = User()
  print(user.field1)
  # &gt; 'value1'
  
  user = User(field1=None)
  print(user.field1)
  # &gt; None
  </code></pre>
  <p><strong>Using Validators for field1 and field2:</strong></p>
  <pre><code>class User(BaseModel):
      account_type: Optional[str] = 'user'
      field1: Optional[str]
      field1: Optional[str]
  
      class Config:
          validate_assignment = True
  
      @validator(&quot;account_type&quot;, pre=True, always=True)
          def _set_account_type(cls, account_type: str):
          return account_type or &quot;user&quot;
      
      @validator(&quot;field1&quot;, pre=True, always=True)
          def _set_plan_credits(cls, value):
              if value is None:
                  value = 'value1'
          return user_dict['account_type']['field1'] or value
  
      @validator(&quot;field2&quot;, pre=True, always=True)
          def _set_rollover_credits(cls, value):
              if value is None:
                  value = 'value2'
          return user_dict['account_type']['field2'] or value
  
  user = User()
  # &gt; AttributeError: type object 'Account' has no attribute 'account_type'
  </code></pre>
  <p>Any ideas on what I can do to get the desired outcome?</p>