I face some troubles serializing an object with Marshmallow-sqlAlchemy.
I have two objects:
class Product(Model):
__tablename__: product_table_name
id = Column(Integer, primary_key=True)
name = Column(String)
class BasketItem(Model):
__tablename__: basket_item_table_name
id = Column(Integer, primary_key=True)
product_id = Column(
Integer, ForeignKey(f"{product_table_name}.id"), nullable=False
)
product = relationship("Product", foreign_keys="BasketItem.product_id")
And here is the marshmallow configuration:
class ProductBasketItemSchema(ModelSchema):
class Meta:
model = Product
fields = ("id", "name",)
class BasketItemSchema(ModelSchema):
class Meta:
model = BasketItem
include_relationships = True
fields = ("id", "product",)
product: Nested(ProductBasketItemSchema, many=False)
But the output of the basket_item_schema.dump(items)
only prints the ID of the products, not the content:
[{'id': 1, 'product': 123}]
instead of
[{'id': 1, 'product': {'id': 123, 'name': 'first'}}]
I think the problem is with the Schema declaration because I can access all the fields of the product before dumping it.
Am I missing something?