如何查询所有产品的变化属性?

发布于 2025-02-08 11:54:24 字数 1782 浏览 1 评论 0 原文

我有以下数据库模式:

“数据库架构”

django模型:

class Product(models.Model):
    name = models.CharField(max_length=150)
    # price and so on

class Size(models.Model):
    value = models.CharField(max_length=20)
    
class Color(models.Model):
    value = models.CharField(max_length=20)

class Variation(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="variations")
    size = models.ForeignKey(Size, on_delete=models.CASCADE)
    color = models.ForeignKey(Color, on_delete=models.CASCADE)

所以我可以写:

product.variations

但是我也希望能够写入

product.sizes

product.colors

以获取该产品在变异表中具有的所有尺寸或颜色的所有尺寸或颜色

。解决方案:我有产品卡列表。并且每张卡都有可以选择的尺寸和颜色的选项,并可以添加到用户的购物车中。我想显示该特定产品在数据库中具有的用户尺寸和颜色,以不列出数据库中的所有尺寸和颜色。

但是变化可以具有重复的内容,例如,考虑某些随机产品的这些组合:

尺寸-40,颜色 - 红色

尺寸-42,颜色 - 绿色

尺寸-44,颜色 - 颜色 - 红色(再次)

大小-42(再次),颜色 - 灰色

我想向用户展示 尺寸:40、42、44

颜色:红色,绿色,灰色,

目前我向所有人展示了所有重复项

:40、42、44、42

颜色:红色,绿色,绿色,红色,灰色,

它是由此产生的代码,我不知道如何重写它:

products: QuerySet[Product] = (
    Product.objects
        .prefetch_related("variations__size")
        .prefetch_related("variations__color")
        .all()[:15]
)

然后我在模板中迭代产品,而不会产生额外的查询

{% for variation in product.variations.all %}
    variation.size.value
{% endfor %}

I have following database schema:

database schema

Django models:

class Product(models.Model):
    name = models.CharField(max_length=150)
    # price and so on

class Size(models.Model):
    value = models.CharField(max_length=20)
    
class Color(models.Model):
    value = models.CharField(max_length=20)

class Variation(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="variations")
    size = models.ForeignKey(Size, on_delete=models.CASCADE)
    color = models.ForeignKey(Color, on_delete=models.CASCADE)

So I can write:

product.variations

But I also want to be able to write

product.sizes

product.colors

to get all sizes or colors that this product has in variation table

The problem that I'm trying to solve: I have product card list. And each card has options of sizes and colors to choose and to add to user's cart. I want to show the user sizes and colors that this particular product has in database to not list all the sizes and colors from database.

enter image description here

But variations can have duplicates, for example, consider these combinations for some random product:

size - 40, color - red

size - 42, color - green

size - 44, color - red (again)

size - 42 (again), color - gray

I want to show the user
sizes: 40, 42, 44

colors: red, green, gray

At the moment I'm showing all of them with duplicates like

sizes: 40, 42, 44, 42

colors: red, green, red, gray

It is produced by this code and I don't know how to rewrite it:

products: QuerySet[Product] = (
    Product.objects
        .prefetch_related("variations__size")
        .prefetch_related("variations__color")
        .all()[:15]
)

Then I iterate over products in my template without producing extra queries like

{% for variation in product.variations.all %}
    variation.size.value
{% endfor %}

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

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

发布评论

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

评论(1

花开柳相依 2025-02-15 11:54:24

看来我已经找到了一个解决方案:

我使用了预取对象及其第三参数to_attr:

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.Prefetch

products: QuerySet[Product] = (
    Product.objects
        .prefetch_related(Prefetch(
            "variations",
            queryset=Variation.objects
                .select_related("size")
                .select_related("color")
                .distinct("color"),
            to_attr="color_variations",
        ))
        .prefetch_related(Prefetch(
            "variations",
            queryset=Variation.objects
                .select_related("size")
                .select_related("color")
                .distinct("size"),
            to_attr="size_variations",
        ))
        .all()[:15]
)

It seems I've found a solution:

I used Prefetch object and its 3rd argument to_attr:

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.Prefetch

products: QuerySet[Product] = (
    Product.objects
        .prefetch_related(Prefetch(
            "variations",
            queryset=Variation.objects
                .select_related("size")
                .select_related("color")
                .distinct("color"),
            to_attr="color_variations",
        ))
        .prefetch_related(Prefetch(
            "variations",
            queryset=Variation.objects
                .select_related("size")
                .select_related("color")
                .distinct("size"),
            to_attr="size_variations",
        ))
        .all()[:15]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文