Toufiqur Rahman
Toufiqur Rahman

Reputation: 212

Show only "Reorder" item

I am using Django and in the front end, I am trying to show only "Reorder" Parts on the table. To define the status of Available and Reorder, I am normally using the if-else method to track. I was thinking about filtering but I have no such Status field in my DB. Anyone can give me any idea how to do that? Here is what I've done for now

HTML

<tr>
   <th class="serial">#</th>
   <th>Part No</th>
   <th>Part Name</th>
   <th>Quantity</th>
   <th>Status</th>
   <th>Date</th>
   <th>Action</th>                                       
</tr>

{% if parts %}
   {% for part in parts %}
   <tr>
      <td class="serial">{{ forloop.counter }}</td> 
      <td>{{ part.partno }}</td>                                      
      <td>{{ part.partname }}</td>
      <td>{{ part.quan }}</td>
      <td>
         {% if part.quan <= part.limit   %}
              <p style="color: #FF0000">
                  Reorder
              </p>
         {% elif part.quan  > part.limit  %}
              <p style="color:#008000">
                  Available
              </p>
         {% endif %}
      </td>
      <td>{{ part.created_date }}</td>
  </tr>

Part Table

Part Table

Upvotes: 0

Views: 30

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

You can reference an value from the instance in a filter, this queryset will return your parts that need reorder

from django.db.models import F
parts = Part.objects.filter(quan__lte=F('limit'))

Upvotes: 1

Related Questions