Skip to content

Hide fields from interfaces #189

Closed Answered by davidjb99
downdawn asked this question in Q&A
Jul 28, 2023 · 3 comments · 3 replies
Discussion options

You must be logged in to vote

Define a base schema and hide any specified fields, as discussed fastapi/fastapi#7585

class MyBase(BaseModel):
    class Config:
        @staticmethod
        def schema_extra(schema: dict, _):
            props = {}
            """
            Removes any fields from the OpenAPI spec if they have a `hidden` tag
            """
            for k, v in schema.get("properties", {}).items():
                if not v.get("hidden", False):
                    props[k] = v
            schema["properties"] = props

class UserCreate(MyBase):
    id: int | None = Field(hidden=True)
   name: str

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

downdawn
Jul 29, 2023
Collaborator Author

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@wu-clan
Comment options

@downdawn
Comment options

downdawn Aug 18, 2023
Collaborator Author

@wu-clan
Comment options

Answer selected by downdawn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants