我正在尝试使用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()为每个唯一商品变体创建1个库存的记录,但这些变体的库存记录不会转换为父商品。
感谢您的任何建议。
编辑: 我已经使用一种方法更新了类,该方法对ProductClass实例显式调用ProductClass.objects.track_stock(),并且在循环遍历CSV文件的所有行之后调用它(将其传递给一个产品类的名称I目前使用)。但是,在仪表板中查看库存时,没有任何子/变体库存计入父项。
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看起来在两个实例中都是正确的。创建child_objects时是否必须为False,然后翻转为True?
编辑:解决方案:
经过test factory的一些研究后,我通过指定
解决了这个问题product.stucture = 'parent'
在父对象上,
product.structure = 'child'
在子对象上。我还需要将对象的自定义属性更改为dict product_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
时默认设置为Product()
答案 0 :(得分:2)
为了能够正确反映任何Product的库存水平,您需要Partner来提供产品,然后您需要StockRecord链接合作伙伴和产品在一起。
首先确保您为Product个变种中的每一个都拥有数据库中的所有信息。
然后您需要更新ProductClass并设置&#34; track_stock&#34;属性为True,因为默认情况下为None。
您还需要从子产品中删除ProductClass,因为他们从其父产品继承了ProductClass。
编辑1:
要向Product添加属性,您必须为ProductClass添加ProductAttribute,然后您可以直接在Product like this example上设置属性。
编辑2:
您还需要设置&#34; net_stock_level&#34;在StockRecord上。
要深入了解奥斯卡如何获得库存水平{@ 3}}。如果您想根据用户收取税款或提供不同的定价,此课程将确定您将来可能需要定制的定价,税金和库存水平策略。