【发布时间】:2021-11-02 04:17:50
【问题描述】:
我正在制作一个非常简单的仓库管理系统,我希望用户能够为商品创建模板。该模板将显示在一个列表中,然后可以单独用于创建项目的实例,该实例还将获得数量和仓库属性。
目标是,如果其中一个商品模板被修改以指定不同的尺寸或价格,则实际商品实例的尺寸或价格属性也会发生变化。
这是我的代码,以帮助您想象我正在尝试做的事情。我不确定这是否可行,或者我是否应该考虑其他解决方案。这是我第一次使用 Flask SQLAlchemy。
class ItemTemplate(db.model):
"""This template will simply store the information related to an item type.
Individual items that will be associated with the warehouse they're stored in
will inherit this information from the item templates."""
_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(15), unique=True, nullable=False)
price = db.column(db.Float, nullable=False)
cost = db.column(db.Float, nullable=False)
size = db.column(db.Integer, nullable=False)
lowThreshold = db.column(db.Integer, nullable=False)
# Actual items
class Item(db.model):
"""This template will be used to represent the actual items that are associated with a warehouse."""
_id = db.Column(db.Integer, primary_key=True)
quantity = db.Column(db.Integer, primary_key=True)
"""Here I want the Item attributes to be able to just point to attributes from the ItemTemplate class.
ItemTemplate(name='tape') <--- will be a template with the information for tape.
Item(name='tape') <--- will be an actually instance of tape that should inherit all the attributes from the tape template.
I want these attributes to be like pointers so that if the tape template has its name changed, for instance, to
'scotch tape', all the Item instances that point to the tape template will have their names changed."""
# Warehouse
class Warehouse(db.model):
_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(15), unique=True, nullable=False)
capacity = db.Column(db.column(db.Integer, nullable=False))
items = db.relationship("Item", backref="warehouse", lazy=True)```
【问题讨论】:
标签: python database flask-sqlalchemy