1.概述
对于一个上述产品,要描述他的信息需要很多属性,我们创建一个数据表去对其进行保存。
1.1使用参数的介绍
例如一个衣服需要的分类参数
cid(描述某个产品) | id(给类型一个编号) | name(类型名称) | value(类型的属性) | type(静态or动态属性) |
---|---|---|---|---|
1 | 1 | 质地 | 棉 | static |
1 | 2 | 风格 | 通勤 | static |
1 | 3 | 尺码 | s,m,l,xl | dynamic |
2.migrate创建数据库
2.1代码展示
# /flask_shop/models.py
# 此文件用于建立数据库表的模型
# 需要针对数据库的模型
# from enum import unique
from flask_shop import db
# 对数据加密 检查密码
from werkzeug.security import generate_password_hash,check_password_hash
from datetime import datetime
# 商品的参数
class Attribute(db.Model):
__tablename__ = 't_attribute'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32))
val = db.Column(db.String(32))
cid = db.Column(db.Integer, db.ForeignKey('t_category.id')) # 外键关联商品分类表
_type = db.Column(db.Enum('static', 'dynamic')) # 枚举规定值
# 返回值
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'val': self.val,
'cid': self.cid,
'type': self._type
}
2.2映射数据库
python manager.py db migrate
python manager.py db upgrade
在category中方便查询 添加一个关联关系,在Class Category
attrs = db.relationship('Attribute', backref='category')
2.3Navicate查看是否创建成功
3.mysql插入数据
3.1代码展示
insert into t_attribute (name,_type,cid,val) value ('品牌','static',65,'soulkiss');
insert into t_attribute (name,_type,cid,val) value ('适用年龄','static',65,'25-29周岁');
insert into t_attribute (name,_type,cid,val) value ('材质','static',65,'蚕丝');
insert into t_attribute (name,_type,cid,val) value ('尺码','static',65,'S M L');
insert into t_attribute (name,_type,cid,val) value ('面料','static',65,'其他');
insert into t_attribute (name,_type,cid,val) value ('图案','static',65,'纯色');
insert into t_attribute (name,_type,cid,val) value ('风格','static',65,'通勤');
insert into t_attribute (name,_type,cid,val) value ('通勤','static',65,'简约');
insert into t_attribute (name,_type,cid,val) value ('领型','static',65,'立领');
insert into t_attribute (name,_type,cid,val) value ('衣门襟','static',65,'单排扣');
insert into t_attribute (name,_type,cid,val) value ('颜色分类','static',65,'冷艳红梨色-100%桑蚕丝 高雅浅杏-100%桑蚕丝 高雅浅杏-100%桑蚕丝-36517批次 冷艳红梨色-100%桑蚕丝-预售 高雅浅杏-100%桑蚕丝-预售 无视洗涤说明概不负责');
insert into t_attribute (name,_type,cid,val) value ('组合形式','static',65,'单件');
insert into t_attribute (name,_type,cid,val) value ('货号','static',65,'S904548');
insert into t_attribute (name,_type,cid,val) value ('成分含量','static',65,'95%以上');
insert into t_attribute (name,_type,cid,val) value ('裙型','static',65,'A字裙');
insert into t_attribute (name,_type,cid,val) value ('年份季节','static',65,'2019年夏季');
insert into t_attribute (name,_type,cid,val) value ('袖长','static',65,'无袖');
insert into t_attribute (name,_type,cid,val) value ('裙长','static',65,'中长裙');
insert into t_attribute (name,_type,cid,val) value ('款式','static',65,'其他/other');
insert into t_attribute (name,_type,cid,val) value ('廓形','static',65,'A型');
insert into t_attribute (name,_type,cid,val) value ('尺码','dynamic',65,'S,M,L');
insert into t_attribute (name,_type,cid,val) value ('颜色分类','dynamic',65,'冷艳红梨色-100%桑蚕丝,高雅浅杏-100%桑蚕丝,高雅浅杏-100%桑蚕丝-36517批次,冷艳红梨色-100%桑蚕丝-预售,高雅浅杏-100%桑蚕丝-预售,无视洗涤说明概不负责');