OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pydantic Settings v2 - getting Field Required Error

  • Thread starter Thread starter Ramakrishnan Srinivasan
  • Start date Start date
R

Ramakrishnan Srinivasan

Guest
mycfg.py

Code:
from pydantic_settings import BaseSettings, SettingsConfigDict

class CommonCfg(BaseSettings):
      SYS_USER: str
      LTDRN_REST_ENDPOINT: str
      FULLRN_REST_ENDPOINT: str
      model_config  = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8', case_sensitive=True, extra='forbid')

class ProductionCfg(CommonCfg):
      HOST: str
      model_config = SettingsConfigDict(env_file='prod.env', env_file_encoding='utf-8', case_sensitive=True, extra='forbid')

#print(CommonCfg.model_config)
#cfg_values = CommonCfg()
cfg_values = ProductionCfg()
print(cfg_values.LTDRN_REST_ENDPOINT)
print(cfg_values.FULLRN_REST_ENDPOINT)
print(cfg_values.SYS_USER)
#print(cfg_values.HOST)

my .env file contains

LTDRN_REST_ENDPOINT=/rest/latest/custom/create_limited_draft_rn FULLRN_REST_ENDPOINT=/rest/latest/custom/create_full_draft_rn

my prod.env file contains

HOST=https://at.host.com

command given SYS_USER=xyzuser python mycfg.py

My understanding was when inherit CommonCfg in ProductionCfg I will get LTDRN_REST_ENDPOINT and FULLRN_REST_ENDPOINT values from .env (kind of master file) and HOST from prod.env file. But I get "Field Required" error.

Traceback (most recent call last):

Code:
  File "/home/build/python-app/path/mycfg.py", line 34, in <module>
    cfg_values = ProductionCfg()
  File "/home/build/python-app/venv/lib/python3.9/site-packages/pydantic_settings/main.py", line 71, in __init__
    super().__init__(
  File "/home/build/python-app/venv/lib/python3.9/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for ProductionCfg
LTDRN_REST_ENDPOINT
  Field required [type=missing, input_value={'HOST': 'https://host...', 'SYS_USER': 'xyzuser'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing
FULLRN_REST_ENDPOINT
  Field required [type=missing, input_value={'HOST': 'https://host...', 'SYS_USER': 'xyzuser'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing

If I set default values in CommonCfg it works but I was trying to go by Inheritance of config from common to production.
<p>mycfg.py</p>
<pre><code>from pydantic_settings import BaseSettings, SettingsConfigDict

class CommonCfg(BaseSettings):
SYS_USER: str
LTDRN_REST_ENDPOINT: str
FULLRN_REST_ENDPOINT: str
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8', case_sensitive=True, extra='forbid')

class ProductionCfg(CommonCfg):
HOST: str
model_config = SettingsConfigDict(env_file='prod.env', env_file_encoding='utf-8', case_sensitive=True, extra='forbid')

#print(CommonCfg.model_config)
#cfg_values = CommonCfg()
cfg_values = ProductionCfg()
print(cfg_values.LTDRN_REST_ENDPOINT)
print(cfg_values.FULLRN_REST_ENDPOINT)
print(cfg_values.SYS_USER)
#print(cfg_values.HOST)
</code></pre>
<p>my .env file contains</p>
<p>LTDRN_REST_ENDPOINT=/rest/latest/custom/create_limited_draft_rn
FULLRN_REST_ENDPOINT=/rest/latest/custom/create_full_draft_rn</p>
<p>my prod.env file contains</p>
<p>HOST=https://at.host.com</p>
<p>command given
SYS_USER=xyzuser python mycfg.py</p>
<p>My understanding was when inherit CommonCfg in ProductionCfg I will get LTDRN_REST_ENDPOINT and FULLRN_REST_ENDPOINT values from .env (kind of master file) and HOST from prod.env file.
But I get "Field Required" error.</p>
<p>Traceback (most recent call last):</p>
<pre><code> File "/home/build/python-app/path/mycfg.py", line 34, in <module>
cfg_values = ProductionCfg()
File "/home/build/python-app/venv/lib/python3.9/site-packages/pydantic_settings/main.py", line 71, in __init__
super().__init__(
File "/home/build/python-app/venv/lib/python3.9/site-packages/pydantic/main.py", line 164, in __init__
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for ProductionCfg
LTDRN_REST_ENDPOINT
Field required [type=missing, input_value={'HOST': '[URL]https://host[/URL]...', 'SYS_USER': 'xyzuser'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.4/v/missing
FULLRN_REST_ENDPOINT
Field required [type=missing, input_value={'HOST': '[URL]https://host[/URL]...', 'SYS_USER': 'xyzuser'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.4/v/missing
</code></pre>
<p>If I set default values in CommonCfg it works but I was trying to go by Inheritance of config from common to production.</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top