from peizhi import *
class Fenlei(db.Model):
__tablename__ = 'fenlei'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(100),nullable=Flask)
lianjie = db.relationship('Good',backref='fenlei')
class Good(db.Model):
__tablename__ = 'good'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(100),nullable=False)
price = db.Column(db.String(100),nullable=False)
picture = db.Column(db.String(8000),nullable=False)
f_id = db.Column(db.Integer,db.ForeignKey(Fenlei.id))
@app.route("/init")
def init():
db.create_all()
return "ok"
@app.route('/uploading',methods=['POST','GET'])
def uploading():
if request.method == 'POST':
fenlei = request.form.get("fenlei")
goods = request.form.get('good')
jiage = request.form.get('price')
pic = request.files.get('pic')
if all([goods,jiage,pic]):
c = Fenlei.query.filter(Fenlei.name==fenlei).first()
if c:
pic_name = photos.save(pic)
pic_url = '/static/upload/' + pic_name
#如果类存在,把商品添加进去
m = Good(name=goods,price=jiage,picture = pic_url)
#把产品添加到类下的连接下
c.lianjie.append(m)
#提交
db.session.commit()
flash("添加成功")
else:
pic_name = photos.save(pic)
pic_url = '/static/upload/' + pic_name
#实例化一个一个分类的对象 并添加新的分类
c_new = Fenlei(name=fenlei)
#添加产品信息赋值变量
m = Good(name=goods,price=jiage,picture=pic_url)
#变量添加到分类下的连接下
c_new.lianjie.append(m)
#把类添加进去
db.session.add(c_new)
#提交
db.session.commit()
flash('成功')
# pic_name = photos.save(pic)
# pic_url = '/tupian/uploading/'+ pic_name
# new_good = Good(name=goods)
# new_good.price =