REST API in Python with FastAPI and pydantic: read-only property in model

2024/4/15 2:01:24

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.

Answer

It is fine to have multiple models. You can use inheritance to reduce code repetition:

from pydantic import BaseModel# Properties to receive via API create/update
class Foo(BaseModel):name: str# Properties to return via API
class FooDB(Foo):id: int

The documentation which is excellent btw!, goes into this more in-depth.

Here is a real user model example taken from the official full stack project generator. You can see how there are multiple models to define the user schema depending on the context.

https://en.xdnf.cn/q/73164.html

Related Q&A

a class with all static methods [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

How can I find null values with SELECT query in psycopg?

I am using psycopg2 library in python and the INSERT query works good when I insert null Value with None, but when I want to do SELECT null values, with None doesnt return any.cur.execute("SELECT …

Pause and continue stopwatch

I am trying to create stopwatch. I have done it but I would like to pause and continue the time whenever I want. I have tried some things but I have no idea how to do it. Is there anybody who would exp…

How do I escape `@` letter from SQL password in connection URI [duplicate]

This question already has an answer here:handle @ in mongodb connection string(1 answer)Closed 9 years ago.when you connect to mongodb using python from SQLAlchamey, we use mongodb://username:password@…

Set WTForms submit button to icon

I want a submit button that displays an icon rather than text. The button is a field in a WTForms form. I am using Bootstrap and Open Iconic for styling and icons. How do I set the submit field to d…

what is the significance of `__repr__` function over normal function [duplicate]

This question already has answers here:Purpose of __repr__ method?(6 answers)Closed 5 years ago.I am trying to learn python with my own and i stucked at __repr__ function. Though i have read lots of p…

Using celery with Flask app context gives Popped wrong app context. AssertionError

Im more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/Im getting the same error message as Create, manage and kill…

How do I reduce the verbosity of chromedriver logs when running it under selenium?

My jenkins failure reports for my functional tests are full of lines like this:selenium.webdriver.remote.remote_connection: DEBUG: Finished Request selenium.webdriver.remote.remote_connection: DEBUG: P…

How to model python properties in UML diagram

What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like i…

Linear regression with tensorflow

I trying to understand linear regression... here is script that I tried to understand: A linear regression learning algorithm example using TensorFlow library. Author: Aymeric Damien Project: https://g…