以编程方式在Django Oscar中创建规范的"父"产品

Tui*_*noe 18 python django python-2.7 django-oscar

我正在尝试使用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()
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我检查了管理页面,只创建了1个ProductClass,它与传递给track_stock()的名称相同.是否还需要其他一些功能才能启用此功能?track_stock()文档有点稀疏.在输出中,track_stock看起来在两个实例中都是正确的.创建child_objects时是否必须为False,然后翻转为True?

编辑:解决方案:

经过测试工厂的一些研究,我通过指定解决了这个问题

product.stucture = 'parent' 
Run Code Online (Sandbox Code Playgroud)

在父对象上,和

product.structure = 'child'
Run Code Online (Sandbox Code Playgroud)

在子对象上.我还需要将对象的自定义属性更改为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)
Run Code Online (Sandbox Code Playgroud)

没有必要为每个父对象创建一个库存记录,因为它们跟踪与它们关联的子对象的库存记录.也没有必要设置track_stock = True,因为True在创建时默认设置为Product()

Ale*_*los 5

为了能够正确反映任何产品的库存水平,您需要有一个供应该产品的合作伙伴,然后您需要有一个将合作伙伴和产品链接在一起的StockRecord 。

首先,确保数据库中拥有每种产品变体的所有信息。

然后您需要更新您的ProductClass并将“ track_stock ”属性设置为 True,因为默认情况下为 None。

您还需要从子产品中删除ProductClass ,因为它们从其父产品继承ProductClass 。

编辑1:

要向产品添加属性,您必须为 ProductClass 添加 ProductAttribute,然后您可以直接在产品上设置属性,如本例所示

编辑2:

您还需要在 StockRecord 上设置“net_stock_level”。

要更深入地了解 Oscar 如何获取库存水平,请查看Selector。如果您想根据用户收取税费或提供不同的定价,此类确定您将来可能需要自定义的定价、税收和库存水平策略。