Suppose I have a studen
t model, and what I want is that: if the age
< 6, make the school
optional.
What can I do? thanks in advance!
from pydantic import BaseModel
class Student(BaseModel):
name: str
age: int
school: str
student = Student(**{ "name": "Tom", "age": 10, "school": "Blabla"}) # validated
baby_student = Student(**{ "name": "Tom", "age": 5}) # expected to be validated
what I have tried:
from pydantic import BaseModel, root_validator
class Student(BaseModel):
name: str
age: int
school: str
@root_validator(pre=True)
def ignore_school_if_baby(cls, values):
age = values.get("age")
if age < 6:
# don't know how to do here
values["school"] = None
return values
baby_student = Student(**{ "name": "Tom", "age": 10})