我有一個名爲Product_Variation模型:Django的選擇僅適用於具有不同領域
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()
的不同也不再因爲ID都是工作獨特
Product_Variation.objects.all()的值( 'product__id', '顏色')不同的()的值( '編號', 'product__id', '顏色')..。嘗試這個 –
nope,奇怪的是它做同樣的事情,像Product_Variation.objects.all()。values('id','product__id','color')。distinct() – RaR
不要在查詢中使用.all()嘗試那 –