我使用了django-oscar
,我需要从一个具有多种颜色,尺寸和纺织品选项的父产品中自动创建产品变体。
我有以下ProductAttributeValues行,用于填充自动创建的变体。
# Basically, a list of dictionaries with 3 keys: 'value', 'attribute', 'product_class'
[
OrderedDict([('value', <QuerySet [<AttributeOption: Black>, <AttributeOption: White>]>, <AttributeOption: Red>]>), ('attribute', <ProductAttribute: dress Color>), ('product_class', 'dress')]),
OrderedDict([('value', <QuerySet [<AttributeOption: M>, <AttributeOption: S>]>), ('attribute', <ProductAttribute: dress Size>), ('product_class', 'dress')]),
OrderedDict([('value', <QuerySet [<AttributeOption: Cotton>, <AttributeOption: Wool>, <AttributeOption: Silk>, <AttributeOption: Nylon>]>), ('attribute', <ProductAttribute: dress Textile>), ('product_class', 'dress')]),
]
我需要获得产品变体的所有组合,如下所示:
# Note product_class is not really needed here.
[
{
<ProductAttribute: dress Color>: <AttributeOption: Black>},
<ProductAttribute: dress Size>: <AttributeOption: S>,
<ProductAttribute: dress Textile>: <AttributeOption: Cotton>,
},
{
<ProductAttribute: dress Color>: <AttributeOption: Black>},
<ProductAttribute: dress Size>: <AttributeOption: S>,
<ProductAttribute: dress Textile>: <AttributeOption: Wool>,
},
....
# this list should include all 24 possible combinations 3*2*4 = 24
#
]
我尝试使用itertools.product
来获得所需的结果,但没有发现运气。需要帮助!谢谢。
以下结果将是更好的设计吗?
[
[
{
"attribute": <ProductAttribute: dress Color>,
"value": <AttributeOption: Black>
},
{
"attribute": <ProductAttribute: dress Size>,
"value": <AttributeOption: S>
},
{
"attribute": <ProductAttribute: dress Textile>,
"value": <AttributeOption: Cotton>
}
],
[
{
"attribute": <ProductAttribute: dress Color>,
"value": <AttributeOption: Black>
},
{
"attribute": <ProductAttribute: dress Size>,
"value": <AttributeOption: S>
},
{
"attribute": <ProductAttribute: dress Textile>,
"value": <AttributeOption: Wool>
}
],
....
# this list should also include all 24 possible combinations 3*2*4 = 24
#
]