本文整理汇总了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)