Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Add products category relationship to products
src/types/group.py | 3 ++- src/types/product.py | 1 + src/types/product_category.py | 7 ++++++-
diff --git a/src/types/group.py b/src/types/group.py index 484c726b772b4de54313296934b2b7f73286d199..7eef507fb01de5b19cda179372b3ecef170e96a9 100644 --- a/src/types/group.py +++ b/src/types/group.py @@ -30,5 +30,6 @@ def to_json(self): return { "id": self.id, "name": self.name, - "location": self.location + "location": self.location, + "products": self.products } diff --git a/src/types/product.py b/src/types/product.py index 432c490de183c5ee394112caf5e824dee59635dc..b85663b1481cba4f4135c9067f075cec97e712d2 100644 --- a/src/types/product.py +++ b/src/types/product.py @@ -7,6 +7,7 @@ id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(256), nullable = False) description = db.Column(db.Text) group_id = db.Column(db.Integer, db.ForeignKey('groups.id'), nullable = False) + category_id = db.Column(db.Integer, db.ForeignKey('product_categories.id'), nullable = False) created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow) diff --git a/src/types/product_category.py b/src/types/product_category.py index 3c506e5dd482edef5a525a6ba28da87c150c1bfb..4d3cfb64482186e2dcdd911a12cf21f97b36c240 100644 --- a/src/types/product_category.py +++ b/src/types/product_category.py @@ -1,5 +1,6 @@ from . import db from datetime import datetime as dt +from .product import Product class ProductCategory(db.Model): __tablename__ = "product_categories" @@ -8,6 +9,9 @@ name = db.Column(db.String(255), nullable = False) description = db.Column(db.Text) created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow) + + products = db.relationship("Product", backref = "category", lazy = True) + def __init__(self, name, description): self.name = name @@ -20,5 +24,6 @@ def to_json(self): return { "name": self.name, - "description": self.description + "description": self.description, + "products": self.products }