我试图使用修改过的 django-oscar import_oscar_catalogue 类从 CSV 中导入一堆产品,在第一次遇到一个产品(由标题定义)时,创建一个规范的父产品,然后在所有未来的遭遇中,在该父产品下创建子产品。
这似乎可行,但是规范产品不反映子产品的组合库存水平,也不显示该产品的正确属性。 但它在 Django 仪表板中正确地将它们列为变体。
如何以编程方式创建产品的子/父关系,并具有正确的库存记录?
相关代码:
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()
函数会为每种商品变体创建一个库存记录,但是这些变体的库存不会转化为其父商品的库存。
编辑:
我已经在类中更新了一个方法,它显式调用ProductClass.objects.track_stock()
对ProductClass实例进行跟踪库存,并在通过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
我已经检查了管理页面,只创建了1个ProductClass,并且它的名称与传递给track_stock()
的名称相同。是否还有其他需要完成以启用此功能的工作?track_stock() 文档有点简略。在输出中,track_stock 在两个实例中都为 true。在创建 child_objects 时,它是否必须为 False,然后再翻转为 True?