New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to configure export arguments? #1387
Comments
Hi @Midnighter ! As it is indeed unavailable in the class MyBaseModel(BaseModel):
def dict(self, by_alias=True, exclude_unset=True, **kwargs):
return super().dict(by_alias=by_alias, exclude_unset=exclude_unset, **kwargs)
def json(self, by_alias=True, exclude_unset=True, **kwargs):
return super().json(by_alias=by_alias, exclude_unset=exclude_unset, **kwargs) class ModelTest(MyBaseModel):
id: str = Field("")
origin_id: str = Field("", alias="originId")
tags: Set[str] = Field(())
m = ModelTest(originId='myid')
print(m.dict())
# > {'originId': 'myid'}
print(m.dict(exclude_unset=False))
# > {'id': '', 'originId': 'myid', 'tags': ()}
print(m.json())
# > {"originId": "myid"} This way it allows you to easily change the default values and maybe one day use a more "native" way. |
Okay, thank you 😃 |
Why was this issue closed? @PrettyWood 's response sounds like a great workaround. But can I still write a PR to change pydantic so future users don't need the workaround? i.e. so that this MWE works:
|
Question
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:I have an application where my models should always be exported with
by_alias=True
andexclude_unset=True
. Similarly to what is proposed in #660, I was looking to add these to the models'Config
but that's not possible at the moment.What is the right way to achieve this? Should I override the
dict
andjson
methods as below?The text was updated successfully, but these errors were encountered: