Reputation: 1
I have model Products
class Products(Base):
__tablename__ = "products"
product_id = Column(Integer, primary_key = True)
name = Column(String, nullable= False)
price = Column(Integer, nullable= False)
description = Column(String)
stars = Column(Integer)
ingredients = Column(String, nullable= False)
nutrition = Column(String, nullable= False)
image_id = Column(Integer)
Parent method that should return object from DB
class BaseService:
model = None
@classmethod
async def find_all(cls):
async with async_session_maker() as session:
query = select(cls.model)
result = await session.execute(query)
return result.mappings().all()
Service.py in Products directory, where all endpoints, model and service of the product are described.
from app.database import async_session_maker
from sqlalchemy import select
from app.products.models import Products
from app.services.base import BaseService
class ProductService(BaseService):
model = Products
That should be return fields as described
class Products(Base):
__tablename__ = "products"
product_id = Column(Integer, primary_key = True)
name = Column(String, nullable= False)
price = Column(Integer, nullable= False)
description = Column(String)
stars = Column(Integer)
ingredients = Column(String, nullable= False)
nutrition = Column(String, nullable= False)
image_id = Column(Integer)
but its returning
Response body
{
"Products": {
"name": "Apple Pie",
"price": 500,
"stars": 4,
"nutrition": "Calories: 250 per slice",
"description": "Delicious homemade apple pie",
"product_id": 1,
"ingredients": "Apples, sugar, flour, cinnamon",
"image_id": null
}
}
In database when i enter select * from products
its returning what i expect
product_id(pk) | name | price | description | stars | ingredients | nutrition | image_id |
---|---|---|---|---|---|---|---|
1 | Apple_pie | 500 | Delicious homemade apple pie | 4 | Apples, sugar, flour, cinnamon | Calories: 250 per slice | None |
Upvotes: 0
Views: 60
Reputation: 1
If you wanna get ordered record - you must use Pydantic model, or manually write mappings.
schema.py
:
from pydantic import BaseModel
class ProductRead(BaseModel):
product_id: int
name: str
price: int
description: str
stars: int
ingredients: str
nutrition: str
image_id: int
Upvotes: 0