产品目录:按参数过滤

发布于 2024-12-07 12:50:57 字数 3386 浏览 5 评论 0原文

我需要按功能制作过滤器产品。

因此,产品模型:

<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>

和特征模型:

<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>

为了标记过滤的模板形式,我需要获取特征的所有可能名称以及该特征的值。

像这样:

颜色:红色、白色、蓝色
尺寸:1、2、3

我希望有人理解我,告诉我如何巧妙地实现功能。谢谢:)

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:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2024-12-14 12:50:57

首先列出给定产品的所有功能:

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

现在直接在 Python 中对功能进行分组。

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

这将为您提供将所有名称链接到其现有值的字典。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文