0

I created a django template with the following model

Models.py

class MaterialRequest(models.Model):
    owner = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='allotment_sales')

    product1 = models.CharField(max_length=500,default=0,blank=True,null=True)
    product1_quantity = models.IntegerField(default=0,blank=True,null=True)

    product2 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product2_quantity = models.IntegerField(default=0,blank=True,null=True)

    product3 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product3_quantity = models.IntegerField(default=0,blank=True,null=True)

    product4 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product4_quantity = models.IntegerField(default=0,blank=True,null=True)

    product5 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product5_quantity = models.IntegerField(default=0,blank=True,null=True)

    product6 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product6_quantity = models.IntegerField(default=0,blank=True,null=True)

    product7 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product7_quantity = models.IntegerField(default=0,blank=True,null=True)

    product8 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product8_quantity = models.IntegerField(default=0,blank=True,null=True)

and I tried displaying this data on the template using this view

def load_salesorder(request):
    so_id = request.GET.get('sales_order')
    s_o = MaterialRequest.objects.filter(pk=so_id)
    print("kits=========",s_o)
    return render(request, 'allotment_so.html', {'sales_order': s_o})

HTML

<table class="table table-bordered">
    <thead>
    <tr>
        <th>Product Short Codes</th>
        <th>Product Quantity</th>
    </tr>
    </thead>

    <tbody>
    <div class="table-container">
        {% for i in sales_order %}
        <tr>
            <td class="align-middle">{{ i.product1 }}</td>
            <td class="align-middle">{{ i.product1_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product2 }}</td>
            <td class="align-middle">{{ i.product2_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product3 }}</td>
            <td class="align-middle">{{ i.product3_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product4 }}</td>
            <td class="align-middle">{{ i.product4_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product5 }}</td>
            <td class="align-middle">{{ i.product5_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product6 }}</td>
            <td class="align-middle">{{ i.product6_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product7 }}</td>
            <td class="align-middle">{{ i.product7_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product8 }}</td>
            <td class="align-middle">{{ i.product8_quantity }}</td>
        </tr>

        {% endfor %}

    </tbody>

</table>

But the problem with is that it also shows the None values of ORM whereas I just want to show the fields that consists the data and leave out the blank values. How can I fix this and also is there a better way to do this ?

Rahul Sharma
  • 1,632
  • 3
  • 19
  • 49

2 Answers2

1

You can achieve that with some if statements in the template:

{% for i in sales_order %}

    {% if i.product1 and i.product1_quantity %}
        <tr>
            <td class="align-middle">{{ i.product1 }}</td>
            <td class="align-middle">{{ i.product1_quantity }}</td>
        </tr>
    {% endif %}

    <!-- same for other fields -->

{% endfor %}
Pedram
  • 3,485
  • 3
  • 17
  • 32
  • and Can I add a for loop after `{% for i in sales_order %}` such that I don't have to repeat the whole code 8 times ? – Rahul Sharma Dec 06 '19 at 10:44
  • 1
    I don't think that's possible via `for` loops, but I'm looking for a better solution. – Pedram Dec 06 '19 at 10:58
  • Please share if you find something.. i tried adding id to the `td` but it certainly didn't feel neat – Rahul Sharma Dec 06 '19 at 11:04
  • @RahulSharma You can try serializing the `sales_order` and then iterate over fields in the serialized object. Check [this](https://stackoverflow.com/a/2311015/9733868) – Pedram Dec 06 '19 at 11:05
1

use can use if statement for each field:

<tr>
    <td class="align-middle">{% if i.product1 %}{{ i.product1 }}{% endif %}</td>
    <td class="align-middle">{% if i.product1_quantity %}{{ i.product1_quantity }}{% endif %}</td>
</tr>
Yeganeh Salami
  • 575
  • 6
  • 29