0

This is my Response model -

class BillBase(BaseModel):
    bill_no: int
    amount: int
    about: str

class ShowSalesBill(BillBase):
    id: int
    left_amount: int

    class Config:
        orm_mode = True

so this left_amount int field is not in the Bills model I want to calculate this field based on some condition. My question is can we get extra fields while getting responses from the response model as we have in the Django serializer we can get some custom data under the get_fiel_name function there is something in FastAPI.

Chris
  • 18,724
  • 6
  • 46
  • 80
mdhv_kothari
  • 627
  • 1
  • 7
  • 14
  • Create a third class that inherits from `ShowSalesBill` and has the extra field – mousetail Jan 17 '23 at 10:57
  • i have to calculate that field according to bill id – mdhv_kothari Jan 17 '23 at 10:58
  • Yes so make a separate class that won't be synced with your database so you can calculate the `left_amount` – mousetail Jan 17 '23 at 10:58
  • Can you give an example ? – mdhv_kothari Jan 17 '23 at 10:59
  • 1
    You could specify a custom `response_model`, as demonstrated in [this answer](https://stackoverflow.com/a/71439821/17865804) and [this answer](https://stackoverflow.com/a/75115378/17865804), adding the extra fields you wish. – Chris Jan 17 '23 at 12:50
  • 1
    Your question is not clear. Please edit it, include a [minimal reproducible example](https://stackoverflow.com/help/mcve) and put some effort into explaining precisely what your desired output is as well as what your current actual output/error is. – Daniil Fajnberg Jan 17 '23 at 21:09
  • @Chris basically I have to run db query to get this left_amount – mdhv_kothari Feb 13 '23 at 16:10

0 Answers0