当前位置: 首页>>代码示例>>Python>>正文


Python Product.category方法代码示例

本文整理汇总了Python中product.Product.category方法的典型用法代码示例。如果您正苦于以下问题:Python Product.category方法的具体用法?Python Product.category怎么用?Python Product.category使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在product.Product的用法示例。


在下文中一共展示了Product.category方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: addNewProduct

# 需要导入模块: from product import Product [as 别名]
# 或者: from product.Product import category [as 别名]
def addNewProduct():
    form = AddNewProductForm()
    if request.method == "POST" and form.validate():
        # Add the new product.
        newProduct = Product(form.name.data, float(form.price.data), int(form.stock.data))

        newProduct.category = form.category.data
        newProduct.description = form.description.data

        db.session.add(newProduct)
        db.session.commit()

        # We should, also, add the product specifications.
        # Specifications are sent as a json string. We do this because it
        # is really hard to generate dynamic for fields on the client with wtforms.
        # The solution is to have a single string field generated with wtforms and
        # to simulate the rest of the string fields on the client, when the client
        # will submit the form, the values of that fields will be collected and saved
        # in that master field. We use a javascript object on the client to track
        # the fields an their values, so at the submission the master field will
        # contain the json representation of that object.
        specifications = json.loads(form.specifications.data)

        for spec_name, spec_value in specifications.iteritems():
            db.session.add(ProductSpecifications(newProduct.id, spec_name, spec_value))
        db.session.commit()

        # Now add the images.
        pictures = json.loads(form.pictures.data)

        for pictureLink in pictures:
            db.session.add(ProductPictures(pictureLink, newProduct.id))
        db.session.commit()

        # Now that the product has an id we can add the rest of the components.
        # First, if the product's category is not already in the database we should add it.
        category = Categories.query.filter_by(name=newProduct.category).first()
        if category is None:
            newCategory = Categories(newProduct.category)
            db.session.add(newCategory)
            db.session.commit()
        else:
            # The product category may exist, but is unavailable, because there
            # are no products available left in it. We should make it available.
            if not category.available:
                category.available = True
                db.session.add(category)
                db.session.commit()

        return redirect(url_for('productAddedSuccessfully', name=newProduct.name))

    flashErrors(form.errors, flash)
    return render_template('add_new_product.html',
                           form=form)
开发者ID:cristid9,项目名称:web-store,代码行数:56,代码来源:views.py


注:本文中的product.Product.category方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。