开发者

Products catalogue: filter by parameters

I need to make the filter products by features.

So, products model:

<pre style='color:#000000;background:#ffffff;'><span style='color:#800000; font-weight:bold; '>class</span> Product<span style='color:#808030; '>(</span>models<span style='color:#808030; '>.</span>Model<span 开发者_高级运维style='color:#808030; '>)</span><span style='color:#808030; '>:</span>
    name <span style='color:#808030; '>=</span> models<span style='color:#808030; '>.</span>CharField<span style='color:#808030; '>(</span>max_length<span style='color:#808030; '>=</span><span style='color:#008c00; '>255</span><span style='color:#808030; '>,</span> unique<span style='color:#808030; '>=</span><span style='color:#e34adc; '>True</span>'<span style='color:#808030; '>)</span>

And feature models:

<pre style='color:#000000;background:#ffffff;'><span style='color:#800000; font-weight:bold; '>class</span> Value<span style='color:#808030; '>(</span>models<span style='color:#808030; '>.</span>Model<span style='color:#808030; '>)</span><span style='color:#808030; '>:</span>
    value <span style='color:#808030; '>=</span> models<span style='color:#808030; '>.</span>CharField<span style='color:#808030; '>(</span>max_length<span style='color:#808030; '>=</span><span style='color:#008c00; '>50</span><span style='color:#808030; '>)</span>

<span style='color:#800000; font-weight:bold; '>class</span> FeatureName<span style='color:#808030; '>(</span>models<span style='color:#808030; '>.</span>Model<span style='color:#808030; '>)</span><span style='color:#808030; '>:</span>
    name <span style='color:#808030; '>=</span> models<span style='color:#808030; '>.</span>CharField<span style='color:#808030; '>(</span>max_length<span style='color:#808030; '>=</span><span style='color:#008c00; '>50</span><span style='color:#808030; '>)</span>

<span style='color:#800000; font-weight:bold; '>class</span> Feature<span style='color:#808030; '>(</span>models<span style='color:#808030; '>.</span>Model<span style='color:#808030; '>)</span><span style='color:#808030; '>:</span>
    name <span style='color:#808030; '>=</span> models<span style='color:#808030; '>.</span>ForeignKey<span style='color:#808030; '>(</span>FeatureName<span style='color:#808030; '>)</span>
    value <span style='color:#808030; '>=</span> models<span style='color:#808030; '>.</span>ForeignKey<span style='color:#808030; '>(</span>Value<span style='color:#808030; '>)</span>
    item <span style='color:#808030; '>=</span> models<span style='color:#808030; '>.</span>ForeignKey<span style='color:#808030; '>(</span>Product<span style='color:#808030; '>)</span>

To mark up a template form of filtering I need to get all the possible names of the characteristics and values ​​of this characteristic.

Like this:

Color: Red, White, Blue

Size: 1, 2, 3

I hope somebody understood me, tell me how to do clever to realize a functional. Thanks:)


Start with listing all Features for given product:

product = Product.objects.get(pk=given_pk)
features = product.feature_set.all().select_related()

Now group your features directly in Python.

features_dict = {}
for feature in features:
    values = features_dict.get(feature.name.name, [])
    features_dict[feature.name.name] = values + [feature.value.value]

That will give you dict linking all name to it's existing values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜