Assume a REST API which defines a POST method on a resource /foos to create a new Foo. When creating a Foo the name of the Foo is an input parameter (present in the request body). When the server creates a Foo it assigns it an ID. This ID is returned together with the name in the REST response. I am looking for something similar to readOnly in OpenAPI.
The input JSON should look like this:
{"name": "bar"
}
The output JSON should look like that:
{"id": 123,"name": "bar"
}
Is there a way to reuse the same pydantic model? Or is it necessary to use two diffent models?
class FooIn(BaseModel):name: strclass Foo(BaseModel):id: intname: str
I cannot find any mentions of "read only", "read-only", or "readonly" in the pydantic documentation or in the Field class code.
Googling I found a post which mentions
id: int = Schema(..., readonly=True)
But that seems to have no effect in my use case.