Python 创建一个规范的;“家长”;Django Oscar中的产品编程
我正在尝试使用该类的修改版本从CSV导入一组产品,在产品的第一次遭遇(由标题定义)时,创建一个规范的父产品,然后为所有未来遭遇在该父产品下创建一个子产品 这似乎是可行的,但规范产品既不能反映子产品的组合库存水平,也不能显示该产品的正确属性。不过,它确实在django仪表板中将它们正确地列为变体 如何以编程方式在产品中创建具有正确库存记录的子/父关系 相关代码:Python 创建一个规范的;“家长”;Django Oscar中的产品编程,python,django,python-2.7,django-oscar,Python,Django,Python 2.7,Django Oscar,我正在尝试使用该类的修改版本从CSV导入一组产品,在产品的第一次遭遇(由标题定义)时,创建一个规范的父产品,然后为所有未来遭遇在该父产品下创建一个子产品 这似乎是可行的,但规范产品既不能反映子产品的组合库存水平,也不能显示该产品的正确属性。不过,它确实在django仪表板中将它们正确地列为变体 如何以编程方式在产品中创建具有正确库存记录的子/父关系 相关代码: def _create_item(self, upc, title, product_class, other_product_attr
def _create_item(self, upc, title, product_class, other_product_attributes):
product_class, __ \
= ProductClass.objects.get_or_create(name=product_class)
try:
parent = Product.objects.get(title=title)
item = Product()
item.parent = parent
except Product.DoesNotExist:
# Here is where I think it might need to be changed
# Maybe pitem = ParentProduct() or something?
pitem = Product()
pitem.upc = upc
pitem.title = title
pitem.other_product_attributes = other_product_attributes
# Here parent item is saved to db
pitem.save()
# Create item because no parent was found
item = Product()
parent = Product.objects.get(title=title)
#Set parent
item.parent = parent
# Customize child attributes
item.product_class = product_class
item.title = title
item.other_product_attributes = other_product_attributes
# Save the child item
item.save()
def _create_stockrecord(self, item, partner_name, partner_sku, price_excl_tax,
num_in_stock, stats):
# Create partner and stock record
partner, _ = Partner.objects.get_or_create(
name=partner_name)
try:
stock = StockRecord.objects.get(partner_sku=partner_sku)
except StockRecord.DoesNotExist:
stock = StockRecord()
stock.num_in_stock = 0
# General attributes
stock.product = item
stock.partner = partner
# SKU will be unique for every object
stock.partner_sku = partner_sku
stock.price_excl_tax = D(price_excl_tax)
stock.num_in_stock += int(num_in_stock)
# Save the object to database
stock.save()
create_stockrecord()为每个唯一的项目变体创建一个包含1个库存的记录,但这些变体的库存记录不会转换为父项目
谢谢你的建议
编辑:
我已经用一个方法更新了这个类,该方法显式地对ProductClass实例调用ProductClass.objects.track_stock(),并在循环遍历CSV文件的所有行之后调用它(将我当前使用的一个产品类的名称传递给它)。但是,当查看“库存”仪表板时,没有任何子项/变体库存被计入父项
def track_stock(self, class_name):
self.logger.info("ProductClass name: %s" % class_name)
product_class = ProductClass.objects.get_or_create(name=class_name)
self.logger.info("ProductClass: %s" % str(product_class))
self.logger.info("TrackStock: %s" % str(product_class[0].track_stock))
product_class[0].track_stock = True
self.logger.info("TrackStock: %s" % str(product_class[0].track_stock))
product_class[0].save()
INFO Starting catalogue import
INFO - Importing records from 'sample_inventory.csv'
INFO - Flushing product data before import
INFO Parent items: 6, child items: 10
INFO ProductClass name: ClassName
INFO ProductClass: (<ProductClass: ClassName>, False)
INFO TrackStock: True
INFO TrackStock: True
在父对象上,以及
product.structure = 'child'
在子对象上。我还需要将对象的自定义属性更改为dictproduct\u attributes
,然后设置对象上的每个值:
if product_attributes:
for code, value in product_attributes.items():
product_class.attributes.get_or_create(name=code, code=code)
setattr(product.attr, code, value)
没有必要为每个父对象创建库存记录,因为它们跟踪与其关联的子对象的库存记录。也没有必要设置
track_stock=True
,因为在创建产品时,默认情况下它被设置为True
(
),以便能够正确反映任何需要的库存水平,您需要一个能够提供产品的仓库,然后您需要一个将合作伙伴和产品链接在一起的仓库
首先,确保数据库中有每个变体的所有信息
然后您需要更新并将“”属性设置为True,因为默认情况下该属性为None
您还需要从子产品中删除,因为它们从父产品继承
编辑1:
要向产品添加属性,必须为ProductClass添加ProductAttribute,然后可以直接在产品上设置属性
编辑2:
您还需要在StockRecord上设置“净库存水平”
更深入地了解奥斯卡是如何获得股票水平的。如果您想根据用户收取税费或提供不同的定价,则此类决定您将来可能需要自定义的定价、税务和库存策略。是,先生,如果您在子产品上有一个product_类,它们的行为会有所不同,如果您在该类上没有track_stock属性,则它不会关心您的库存记录。关于动态设置属性,您需要将属性添加到ProductClass,然后创建ProductAttributeValue。我在答案中添加了更多信息。如果您还有其他问题,请告诉我。您可能还需要在库存记录上设置“净库存水平”。我认为您不需要母产品的库存记录。只要子项具有此值,您就应该能够看到父产品的库存计数。请告诉我您是否能够正确显示规范产品的库存记录计数。
if product_attributes:
for code, value in product_attributes.items():
product_class.attributes.get_or_create(name=code, code=code)
setattr(product.attr, code, value)