2017-05-08 46 views
0

我有一個名爲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都是工作獨特

+0

Product_Variation.objects.all()的值( 'product__id', '顏色')不同的()的值( '編號', 'product__id', '顏色')..。嘗試這個 –

+0

nope,奇怪的是它做同樣的事情,像Product_Variation.objects.all()。values('id','product__id','color')。distinct() – RaR

+0

不要在查詢中使用.all()嘗試那 –

回答

0

事情是這樣的:

distinct_prod_vars = Product_Variation.objects.all().distinct('color') 
+0

不傷心。 – RaR

+0

我更新了問題 – RaR

+0

我想你將不得不使用兩個查詢集來獲取你正在尋找的結果! –

0

嘗試:

distinct_prod_vals = Product_Variation.objects.values('color').distinct() 
+0

我試過了。關閉,但我也需要product_variation ID。謝謝 – RaR

+0

@RaymondSeger:我已經更新了答案,嘗試 –

+0

我試過Product_Variation.objects.all()。values('product__id','color')。distinct() 但我不知道如何獲取該ID也是,因爲如果我使用Product_Variation.objects.all()。values('id','product__id','color')。distinct() distinct不再工作,因爲id全都是唯一的 – RaR

相關問題