'Pydantic: Disable root_validator in child class
Is it possible to disable or remove or replace superclass's root validator in pydantic for child classes?
Here is an example:
from pydantic import BaseModel, root_validator
def parent_validator(cls, values):
print('parent validator triggered')
return values
def child_validator(cls, values):
print('child validator triggered')
return values
class ParentModel(BaseModel):
name: str
_custom_validator = root_validator()(parent_validator)
class ChildModel(ParentModel):
_custom_validator = root_validator()(child_validator)
m1 = ParentModel(name='example name')
# outputs:
# parent validator triggered
m2 = ChildModel(name='example name 2')
# outputs:
# parent validator triggered
# child validator triggered
Expected behaviour:
m2 = ChildModel(name='example name 2')
# outputs:
# child validator triggered
A perfect solution for me would be something like this:
class ChildModel(ParentModel):
_custom_validator = None
but it doesn't seem to work.
Tried with pydantic==1.9.1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
