会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132808个问题
Python 全系列/第一阶段:Python入门/控制语句 31906楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 31907楼

vue_shop.7z

这是前端代码,麻烦老师解决一下,谢谢!

Python 全系列/第九阶段:Flask百战电商后台系统/Flask百战电商后台项目 31909楼

后端会报错的代码:

# 商品管理Model的创建

class Goods(db.Model):

    __tablename__ = 't_goods'

    id = db.Column(db.Integer,primary_key=True)

    name = db.Column(db.String(512))

    price = db.Column(db.DECIMAL(20,4))  # 这里使用db.DECIMAI(20,4),在获取商品列表时会报错 TypeError: Object of type Decimal is not JSON serializable

    number = db.Column(db.Integer)

    introduce = db.Column(db.Text)

    big_log = db.Column(db.String(256))

    small_log = db.Column(db.String(256))

    state = db.Column(db.Integer) # 0 未通过 1 审核中 2 已审核

    is_promote = db.Column(db.Integer)

    hot_number = db.Column(db.Integer)

    weight = db.Column(db.Integer)

    cid_one = db.Column(db.Integer,db.ForeignKey('t_category.id'))

    cid_two = db.Column(db.Integer,db.ForeignKey('t_category.id'))

    cid_three = db.Column(db.Integer,db.ForeignKey('t_category.id'))


    category = db.relationship('Category',foreign_keys=[cid_three])


    def to_dict(self):

        return {

           'id':self.id,

            'name':self.name,

            'price':self.price,

            'number':self.number,

            'introduce':self.introduce,

            'big_log':self.big_log,

            'small_log':self.small_log,

            'state':self.state,

            'is_promote':self.is_promote,

            'hot_number':self.hot_number,

            'weight':self.weight,

            'cid_one':self.cid_one,

            'cid_two':self.cid_two,

            'cid_three':self.cid_three,

            'attrs':[a.to_dict() for a in self.category.attrs]

        }

之所以price设置为decimal类型而不是float类型,是因为float会丢失精度,在涉及金额时一般设置为decimal类型


Python 全系列/第九阶段:Flask百战电商后台系统/Flask百战电商后台项目 31910楼
Python 全系列/第九阶段:Flask百战电商后台系统/Flask百战电商后台项目 31911楼
WEB前端全系列/第二阶段:JavaScript编程模块/字符串与数组 31912楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 31917楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637