class ProductEntry(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="product_entries")
price = models.DecimalField(max_digits=8, decimal_places=0)
quantity = models.PositiveIntegerField(default=0)
image = models.ForeignKey(Image, on_delete=models.PROTECT, related_name="product_entries", blank=True, null=True)
variants = models.ManyToManyField(Variant)
inventory_management = models.BooleanField(default=True)
def get_variants(self):
print(self.variants.values())
get_variants返回bottom结果:<QuerySet [{'id': 3, 'name': 'Red', 'image_id': None, 'color': '#C80000FF', 'category': 'C'}, {'id': 1, 'name': 'Black', 'image_id': None, 'color': '#1F1F1FFF', 'category': 'C'}]> <QuerySet [{'id': 2, 'name': 'Purple', 'image_id': None, 'color': '#2D1952FF', 'category': 'C'}]> <QuerySet [{'id': 3, 'name': 'Red', 'image_id': None, 'color': '#C80000FF', 'category': 'C'}]> <QuerySet [{'id': 4, 'name': 'Green', 'image_id': None, 'color': '#009812FF', 'category': 'C'}]>
结果,红色已经重复了两次,我想删除其中一个self.variants.values().distinct("name")
1条答案
按热度按时间6vl6ewon1#
你可以通过以下方式获得不同颜色的迭代对象: