Skip to content
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 exclude fields from the model itself when exporting them? (instead of dict(exclude=..)) #1862

Closed
arielb135 opened this issue Aug 23, 2020 · 2 comments
Labels

Comments

@arielb135
Copy link

arielb135 commented Aug 23, 2020

Basically, i have the following model:

class MyCls(BaseModel):
    permissions: Permissions
    some_id: str = Field('')
    some_flag: bool = Field(False)

when exporting this module to dict / anything else - i want to exclude some_flag from the output.
I know it can be done through the export in the dict method - but this class is a subclass in a more complex model, and i don't want the user of the class to decide what to exclude.

Note - i'm using this flag for some inner calculations.

@PrettyWood
Copy link
Member

Hello @arielb135
Currently it's not possible without overriding dict method (or any other workaround).
But I think support of private attributes or having a special value of dump alias (like dump_alias=None) to exclude fields would be two viable solutions.
Both solutions may be included in pydantic 1.7 if everything goes well.
WDYT ?

@daviskirk
Copy link
Contributor

I think this might be a duplicate of #660 right?
The solution (I think ... when that's done) would look something like:

class MyCls(BaseModel):
    permissions: Permissions
    some_id: str = Field('')
    some_flag: bool = Field(False)

    class Config:
        fields = {
            'some_flag': {
                'exclude': ...,
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants