question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Dump only" and "load only" fields

See original GitHub issue

Feature 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:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
tiangolocommented, May 3, 2019

@haizaar about the _id and _version, I think you can just use alias:

class User(BaseModel):
    id: Optional[int] = Schema(None, alias="_id")
    version: str = Schema(None, alias="_version")
    name: str
0reactions
samuelcolvincommented, Jun 27, 2019

I think this is all covered by other issues, specifically #624.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Dump only" and "load only" fields · Issue #477 · pydantic ...
"dump only" fields can be useful in situations when we don't want clients to modify some things like ids of some entities or...
Read more >
How to define the same field for load_only and dump_only ...
The trick is to define your field in load_fields and dump_fields inside __init__ method. from marshmallow.fields import Integer, String, Raw ...
Read more >
Quickstart — marshmallow 3.19.0 documentation
When loading, dump-only fields are considered unknown. If the unknown option is set to INCLUDE , values with keys corresponding to those fields...
Read more >
great_expectations.marshmallow__shade.schema
dump_only – Fields to skip during deserialization (read-only fields) ... Same as load() , except it takes a JSON string as input. Parameters....
Read more >
Source code for marshmallow.schema - MindMeld
:param load_only: Fields to skip during serialization (write-only fields) :param dump_only: Fields to ... :param many: Value of ``many`` on dump or load....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found