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 Dependent Schema

  • Thread starter Thread starter tariqjamal1544
  • Start date Start date
T

tariqjamal1544

Guest
Code:
class LocationRequest(BaseModel):
    business_unit: Optional[str] = None
    opening: Optional[int]
    max_applicant: Optional[int]
    diversity_male: Optional[int]
    diversity_female: Optional[int]


class JobRoleRequest(BaseModel):
    name: str
    description: Optional[str] = None
    work_mode: Literal["full time", "half day"]


class JobRoleCreateRequest(JobRoleRequest):

    location: List[LocationRequest]

while adding null value for integer fields in sub-schema location: List[LocationRequest] but for business_unit when i leave it is working

Need possible ways to add null value for sub-schema fields

Sample Request schema

Code:
{
    "name": "Job role",
    "description": "job role description",
    "work_mode": "Remote",
    "location": [
        {
            "business_unit": "busiess_unit of company 1",
            "opening": 10,
            "max_applicant": 4,
            "diversity_male": 2
            "diversity_female": 2,
        }
    ]
}

If I pass the location list without optional fields like max_applicant or diversity_male, it should work. However, when I try to pass it without these fields, I'm getting an error.
<pre><code>class LocationRequest(BaseModel):
business_unit: Optional[str] = None
opening: Optional[int]
max_applicant: Optional[int]
diversity_male: Optional[int]
diversity_female: Optional[int]


class JobRoleRequest(BaseModel):
name: str
description: Optional[str] = None
work_mode: Literal["full time", "half day"]


class JobRoleCreateRequest(JobRoleRequest):

location: List[LocationRequest]

</code></pre>
<p>while adding null value for integer fields in sub-schema location: <code> List[LocationRequest]</code> but for business_unit when i leave it is working</p>
<p>Need possible ways to add null value for sub-schema fields</p>
<p>Sample Request schema</p>
<pre><code>{
"name": "Job role",
"description": "job role description",
"work_mode": "Remote",
"location": [
{
"business_unit": "busiess_unit of company 1",
"opening": 10,
"max_applicant": 4,
"diversity_male": 2
"diversity_female": 2,
}
]
}
</code></pre>
<p>If I pass the location list without optional fields like max_applicant or diversity_male, it should work. However, when I try to pass it without these fields, I'm getting an error.</p>
 
Top