ref: products
src/types/product_category.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from . import db from datetime import datetime as dt from .product import Product class ProductCategory(db.Model): __tablename__ = "product_categories" id = db.Column(db.Integer, primary_key = True) 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 self.description = description def create(self): db.session.add(self) db.session.commit() def to_json(self): return { "name": self.name, "description": self.description, "products": list(map(lambda p: p.to_json(), self.products)) } |