'FastAPI Crud design
I'm working on a Restful API using FastAPI and pydantic and postgres. I'm looking for schema design that will allow me to get data from user in a POST\PUT requests, enrich the data (default values that shouldn't be set by user or other columns that are deduced by given data), and then insert it to DB.
The project generator (see reference) suggests using schemas UserCreate\UserUpdate etc. as input to POST \ PUT routes. These objects are passed, in turn, to crud class method that creates\updates the object in the DB. This design will not work since the crud class method needs more information.
Example: In order to create a user, name, height and weight are required.
class UserCreate(BaseMethod):
name: str
height: integer
weight: float
User model in database
class User(Model):
name = Column()
height = Column()
weight = Column()
bmi = Column()
Crud method need to get name, height, weight and BMI. How do I design my schemas in a way that will make this process as clean as possible?
PS: This is a simple example, I'm not looking for a solution in Database level (I guess that in some databases I can automatically update a column based on other column).
Project Generator Reference: https://github.com/tiangolo/full-stack-fastapi-postgresql/tree/master
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
