Skip to content

Is there a way to have a single field be static and immutable with pydantic #1927

@bb-rrogers

Description

@bb-rrogers

What I currently have is a class like so:

class Test(BaseModel):
   id: str = None
   name: str = None
   system_url: str

What I am trying to do is have it so that system_url is a static value that cannot be changed and shouldn't be a value that is accepted as being passed in.

I have tried to create a subclass also, however it still changes the variable if it's passed in on new instance creation.

I have thought of using a validator that will ignore the value and instead set the system property that I plan on using. However am looking for other ways that may support this. I have tried to search if this has come up before but constantly run into the JSONSchema.const field type that I feel doesn't match with what I am trying to achieve.

Your help here is greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions