Reputation: 139
first of all, I cant find the way to explain what I'm intend to do properly so I'll try to do it the best way I can, hope it make sense to you.
I'm trying to display on my index page a list of items that meet a certain criteria. I am creating an inventory app and I want to get a list of all items that need to be resupplied.
views.py
def index(request):
template_name = 'inventario/index.html'
model = Item
# GETS ALL ALCTIVE ITEMS
articulos = Item.objects.all().filter(activo=True)
for i in articulos:
# PRINTS THE VALUE OF 'cantidad_existente' AND 'cantidad_minima' ON EACH ITERATION
print(F'--->>>>>>{i.cantidad_existente}<<<<<<<---CANTIDAD EXISTENTE')
print(F'--->>>>>>{i.cantidad_minima}<<<<<<<---CANTIDAD MINIMA')
# TRIES TO GET FILTERD VALUE FOR EACH ITEM
articulo = Item.objects.filter(cantidad_existente__lte=i.cantidad_minima)
print(F'--->>>>>>{articulo}<<<<<<<---')
# print(F'--->>>>>>{articulo}<<<<<<<---')
return render(request, template_name)#, {'items': items})
results in terminal
--->>>>>>6<<<<<<<---CANTIDAD EXISTENTE
--->>>>>>20<<<<<<<---CANTIDAD MINIMA
--->>>>>><QuerySet [<Item: Rondana 1/8>, <Item: Cortador Carburo 1/8>]><<<<<<<---
--->>>>>>2<<<<<<<---CANTIDAD EXISTENTE
--->>>>>>5<<<<<<<---CANTIDAD MINIMA
--->>>>>><QuerySet [<Item: Cortador Carburo 1/8>]><<<<<<<---
So I can see that every iteration brings back cantidad_minima and cantidad_existente for each item in the queryset. My reasoning is that every iteration it would query my database bring back my result i wanted but its not. what am i doing wrong and how can i go about this issue
UPDATE:
I have made some changes to my query and now its working the way I wanted to (sorts of).
updated function on views.py
def index(request):
template_name = 'inventario/index.html'
model = Item
# GETS ALL ALCTIVE ITEMS
articulos = Item.objects.all().filter(activo=True)
l = []
for i in articulos:
# PRINTS THE VALUE OF 'cantidad_existente' AND 'cantidad_minima' ON EACH ITERATION
print(F'--->>>>>>{i.cantidad_existente}<<<<<<<---CANTIDAD EXISTENTE')
print(F'--->>>>>>{i.cantidad_minima}<<<<<<<---CANTIDAD MINIMA')
# TRIES TO GET FILTERD VALUE FOR EACH ITEM
articulo = Item.objects.filter(cantidad_existente__lte=i.cantidad_minima).filter(pk=i.pk)
if articulo:
l.append(articulo)
print(F'--->>>>>>{articulo}<<<<<<<---')
print(F'--->>>>>>{l}<<<<<<<---')
return render(request, template_name, {'items': l})
now the problem I have is how to display the elements on my template, how can I iterate over a list to get an item in a queryset?
results in terminal
--->>>>>>21<<<<<<<---CANTIDAD EXISTENTE
--->>>>>>20<<<<<<<---CANTIDAD MINIMA
--->>>>>><QuerySet []><<<<<<<---
--->>>>>>4<<<<<<<---CANTIDAD EXISTENTE
--->>>>>>5<<<<<<<---CANTIDAD MINIMA
--->>>>>><QuerySet [<Item: Cortador Carburo 1/8>]><<<<<<<---
UPDAtE 2: Adding models
I'm using Item that has all common field to all tools in my inventory and this class gets inherited by other classes that have field unique to a specific tool.
class Item(models.Model):
description = models.CharField(max_length=100,)
numero_parte = models.CharField(max_length=100)
proveedor = models.ForeignKey(Proveedor, on_delete=models.CASCADE)
cantidad_existente = models.PositiveIntegerField()
update = models.PositiveIntegerField(blank=True, default=0)
cantidad_minima = models.PositiveIntegerField()
precio_unitario = models.DecimalField(max_digits=7, decimal_places=2)
total = models.DecimalField(max_digits=7, decimal_places=2, blank=True)
# asignado_a = models.ForeignKey(Empleados, on_delete=models.CASCADE, blank=True, null=True)
anaquel = models.CharField(max_length=2, choices=ANAQUEL, blank=True, null=True)
posicion_en_x = models.CharField(max_length=2, blank=True, null=True)
posicion_en_y = models.CharField(max_length=2, blank=True, null=True)
activo = models.BooleanField()
def save(self,*args,**kwargs):
self.total = self.cantidad_existente * self.precio_unitario
super().save(*args,**kwargs)
def __str__(self):
return self.description
class Cortadores(Item):
tipo = models.ForeignKey(Tipos_Cortadores,on_delete=models.CASCADE)
material = models.ForeignKey(Materiales, on_delete=models.CASCADE)
filos = models.CharField(max_length=10, choices=GABILANES)
diametro = models.ForeignKey(Diametros, on_delete=models.CASCADE)
longitud = models.ForeignKey(Longitud, on_delete=models.CASCADE)
desbaste = models.CharField(max_length=1, choices=DESBASTE)
class Meta:
verbose_name_plural = "Cortadores"
def get_absolute_url(self):
return reverse('inventario:cortadores-list', kwargs={'id': self.tipo.id})
def __str__(self):
return '%s %s %s %s %s %s' % ( str(self.tipo), str(self.material), str(self.filos), str(self.diametro),
self.longitud, self.desbaste
)
index.html
{% block lista %}
<div class="container cf">
<div class="lista">
{% if items %}
<table id="tablas" class="cf">
<tr class="theader">
<th>Tipo</th>
<th>Descripcion </th>
<th>No. Parte</th>
<th>Proveedor</th>
<th>C. Existente</th>
<th>C. Minima </th>
<th>Locación</th>
</tr>
{% for c in items %}
<tr class="list-index" >
<td>{{c.cortadores.tipo.c}}</td>
<td>{{c.description}}</td>
<td>{{c.numero_parte}}</td>
<td>{{c.proveedor}}</td>
<td>{{c.cantidad_existente}}</td>
<td>{{c.cantidad_minima}}</td>
<td>Anaquel: {{c.anaquel}} | {{c.posicion_en_x}} | {{c.posicion_en_y}}</td>
</tr>
{% endfor%}
</table>
{% else %}
<h1>No hay articulos por vencer</h1>
{%endif%}
</div>
</div>
{% endblock lista%}
Upvotes: 0
Views: 187
Reputation: 1540
try this
articulos = Item.objects.all().filter(activo=True,)
l = []
for i in articulos:
# TRIES TO GET FILTERD VALUE FOR EACH ITEM
articulo = Item.objects.filter(pk=i.pk,cantidad_existente__lte=i.cantidad_minima)
if articulo:
l.append(articulo[0])
return render(request, template_name, {'items': l})
Upvotes: 0
Reputation: 1254
Since you are sending your list as context, so you can just iterate over the list in your template
{% for item in items %}
<tr>
<td>{{ item.0 }}</td>
<td>{{ item.1 }}</td>
...
</tr>
{% endfor %}
If that doesn't work try adding the field names after the index like {{ item.0.description }}
Upvotes: 1