1

I have 2 tables in my database product and skuoption, the skuoption table has variant_id and this variant_id is storing multiple times with the same fields in skuoption table, please let me know how I can remove the duplicate value of variant_id when I fetch data on view page.

here is my code for product-view.html file...

  <h3>₹ {{product.saleprice}}</h3>
                       <div class="product-description border-product">
                            <div class="size-box">
                                <ul><b>Select Size: </b>
                                    {% for sku in product.skuoption_set.all %}
                                    <li><a href="javascript:void()">{{sku.variantsize}}</a></li>
                                    {% endfor %}
                                </ul>
                            </div></div>

what condition i can add here ({% for sku in product.skuoption_set.all %}) or here {{sku.variantsize}}) so that i can remove duplicate values from my view page. Please have a look in the screenshot, which duplicate value i want to remove. enter image description here

Here is my `models.py file...

class Size(models.Model):
    variant=models.ForeignKey('Variants', related_name='var_size', on_delete=models.CASCADE, null=True, blank=True)
    name=models.CharField(max_length=285)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now=True)

    def __str__(self):
       return self.name

this is for skuoption table

class SkuOption(models.Model):
    product = models.ForeignKey('Product', null=True, blank=True, on_delete=models.CASCADE)
    variantflavour=models.ForeignKey('Flavour', related_name='varflavour', on_delete=models.CASCADE, null=True, blank=True)
    variantsize=models.ForeignKey('Size', related_name='varsize', on_delete=models.CASCADE, null=True, blank=True)
    sku=models.CharField(max_length=285, null=True, blank=True)
    price=models.CharField(max_length=285, null=True, blank=True)
    qty=models.CharField(max_length=285, null=True, blank=True)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.sku
2
  • Try using distinct(): for sku in product.skuoption_set.distinct.all Commented Jul 21, 2020 at 12:53
  • it's not working, do u have any other solution. Commented Jul 21, 2020 at 12:59

1 Answer 1

1

you can query in your view function

variantsizes = product.skuoption_set.values('variantsize__name').distinct()
variantsizes_list = [rec['variantsize__name'] for rec in variantsizes]
context = {'variantsizes_list':variantsizes_list}

template

<div class="product-description border-product">
    <div class="size-box">
        <ul><b>Select Size: </b>
            {% for size in variantsizes_list %}
            <li><a href="javascript:void()">{{size}}</a></li>
            {% endfor %}
        </ul>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

14 Comments

it's working perfect, but it's giving me the variantsize_id in numeric format, how I can get the actual name of variantsize_id, because variantsize_id=1 is coming from variantsize table.
I didn't get you. Please provide your output and model stucture
Please check this screenshot after opening this URL...https://prnt.sc/tlxp1z
what field did you provide in values?
Please check my both table in these screenshot https://prnt.sc/tlxrcy and with relationship https://prnt.sc/tlxqd7
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.