我有以下数据库模式:
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:

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.

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 %}
发布评论
评论(1)
看来我已经找到了一个解决方案:
我使用了预取对象及其第三参数to_attr:
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.Prefetch
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