"Dump only" and "load only" fields
See original GitHub issueFeature request
I guess, it would be useful to have an option to create “dump only” and “load only” fields in BaseModels.
“load_only” fields can be often used in web APIs in cases where we have some information we don’t want to expose in our api Examples of usage:
- users’ passwords
- fields with models for internal usage: e.g. we have models which are irrelevant for clients, but these models do some work in api handlers
“dump only” fields can be useful in situations when we don’t want clients to modify some things like ids of some entities or hashed passwords.
In my opinion, it should look like this:
from pydantic import BaseModel
class MyModel(BaseModel):
a: int
b: str
c: bool
class Config:
load_only = {"a"}
dump_only = {"b"}
I wanted to add this feature in #471 and @samuelcolvin thinks that it is rarely needed, but if lots of people vote it up, he would accept this PR, so let me know if it is useful.
Also, in my opinion, it is more convenient than nested excludes because if we want to reuse some of our models, we would always have to keep exclude dict somewhere, but I think that the information about excludes is relevant to the model itself. Also, the information about load_only and dump_only fields can be obtained in some external tools like API documentation and other things
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:10 (10 by maintainers)
@haizaar about the
_id
and_version
, I think you can just usealias
:I think this is all covered by other issues, specifically #624.