For example...suppose I have the code:
from pydantic import BaseModel
class User(BaseModel):
a: int
b: dict
c: str
User(**{"a": "2", "b": "gibberish", "c": "ok"}).dict() # should give {"a": 2, "b": "gibberish", "c": "ok"}
Is this achievable with Pydantic? I've tried defining custom validators (w/ all sorts of configurations...using pre=True, root validators w/ or w/out pre=True, etc) but nothing seems to work.
CodePudding user response:
Use construct to create models without validation:
from pydantic import BaseModel
class User(BaseModel):
a: int
b: dict
c: str
user_dict = User.construct(**{"a": "2", "b": "gibberish", "c": "ok"}).dict()
print(user_dict) # {'a': '2', 'b': 'gibberish', 'c': 'ok'}
CodePudding user response:
As per https://github.com/samuelcolvin/pydantic/discussions/4284, this is not easily configurable via Pydantic V1. The only option (according to the author themselves) is to define custom data types and call the validators directly and then catch the exception.