我有一个名为Product_Variation的模型:
class Product_Variation( models.Model ):
color = models.ForeignKey('Color', verbose_name="Color", on_delete=models.CASCADE, null=True, blank=True)
size = models.ForeignKey('Size', verbose_name="Size", on_delete=models.CASCADE, null=True, blank=True)
sku = models.CharField(verbose_name="SKU", max_length=255, null=True, blank=True)
main_picture = FilerImageField(related_name="main_picture", verbose_name="Main Picture", null=True, blank=True)
image_gallery = models.ManyToManyField('Media', related_name="image_gallery", verbose_name="Image Gallery", blank=True)
regular_price = models.FloatField(verbose_name="Regular Price", null=True, blank=True)
sale_price = models.FloatField(verbose_name="Sale Price", null=True, blank=True)
stock_quantity = models.PositiveIntegerField(verbose_name="Stock Quantity", default=0, null=True, blank=True)
weight = models.FloatField(verbose_name="Weight", default=0, null=True, blank=True)
dimension_length = models.FloatField(verbose_name="Length", default=0, null=True, blank=True)
dimension_width = models.FloatField(verbose_name="Width", default=0, null=True, blank=True)
dimension_height = models.FloatField(verbose_name="Height", default=0, null=True, blank=True)
barcode = models.CharField(verbose_name="Barcode", max_length=255, null=True, blank=True)
priority = models.PositiveIntegerField(verbose_name="Priority", null=True, blank=True)
total_view = models.PositiveIntegerField(verbose_name="Total View", default=0, null=True, blank=True)
total_sales = models.PositiveIntegerField(verbose_name="Total Sales", default=0, null=True, blank=True)
created = models.DateTimeField(default=now)
product = models.ForeignKey('Product', verbose_name="Product that Variation belongs to", on_delete=models.CASCADE, null=True, blank=True)
如何获得具有不同颜色的Product_variations?我正在使用mysql。
我试过了:
Product_Variation.objects.all().values('product__id', 'color').distinct()
但我不知道如何只获取id,因为如果我使用
Product_Variation.objects.all().values('id', 'product__id', 'color').distinct()
distinct不再起作用,因为id都是唯一的
答案 0 :(得分:0)
试试这个:
distinct_prod_vals = Product_Variation.objects.values('color').distinct()