2017-05-08 73 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