Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge multiple models into one common schema in Fastapi

I recently started studyingfastapi and can’t serialize response schema.

Got two model object, but I could not bring it to a specific scheme.

Without using response_model, I just get the following response:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "OcProduct": {

    "viewed": 617,
    "product_id": 160,
    "model": "name",
    "stock_status_id": 6,
    "image": "catalog/product/test.jpg",
    "status": 1
  },
  "OcStockStatu": {
    "name": "Out Of Stock",
    "language_id": 2,
    "stock_status_id": 6
  }
}

But I want to get this:

{
    "viewed": 617,
    "product_id": 160,
    "model": "name",
    "stock_status_id": 6,
    "image": "catalog/product/test.jpg",
    "status": 1,
    "name": "Out Of Stock"
}

orm_product.py

def get_product_by_id(product_id: int, db: Session):
    return db.query(OcProduct, OcStockStatu).join(OcStockStatu, OcProduct.stock_status_id == OcStockStatu.stock_status_id).filter(OcProduct.product_id == product_id).first()

models.py

class OcProduct(Base):
    __tablename__ = 'oc_product'

    product_id = Column(Integer, primary_key=True)
    model = Column(String(64), nullable=False)
    stock_status_id = Column(Integer, nullable=False)
    image = Column(String(255))
    status = Column(TINYINT(1), nullable=False, server_default=text("'0'"))
    viewed = Column(Integer, nullable=False, server_default=text("'0'"))

class OcStockStatu(Base):
    __tablename__ = 'oc_stock_status'

    stock_status_id = Column(Integer, primary_key=True, nullable=False)
    language_id = Column(Integer, primary_key=True, nullable=False)
    name = Column(String(32), nullable=False)

scheme_product.py

class DetailProduct(BaseModel):

    product_id: int
    model: str
    image: str
    price: float
    name: str

    class Config:
        orm_mode = True

product.py

@router.get("/product/{id}", response_model=scheme_product.DetailProduct)
def get_detail_product(id: int, db: Session = Depends(get_db)):
    product = orm_product.get_product_by_id(id, db)
    .......
    return product

With the given schema, I get the following exception:

Output

raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 5 validation errors for DetailProduct
response -> product_id
  field required (type=value_error.missing)
response -> model
  field required (type=value_error.missing)
response -> image
  field required (type=value_error.missing)
response -> price
  field required (type=value_error.missing)
response -> name
  field required (type=value_error.missing)

How can I solve this problem?

>Solution :

While writing query mention the fields you want.

Example

db.query(OcProduct.field1, OcProduct.field2, OcStockStatu.field1, OcStockStatu.field2).join(OcStockStatu, OcProduct.stock_status_id == OcStockStatu.stock_status_id).filter(OcProduct.product_id == product_id).first()

field1 and field2 are examples. Select fields which you want from related table as above. (product_id, model and so on.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading