首先,我找不到方法来解释我打算正确做的事情,因此我将尽我所能尽力做到这一点,希望对您有意义。
我正在尝试在索引页面上显示符合特定条件的项目列表。我正在创建一个清单应用程序,并且想要获取需要重新供应的所有物品的清单。
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})
终端结果
--->>>>>>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>]><<<<<<<---
因此,我可以看到每次迭代都为查询集中的每个项目带来了cantidad_minima和cantidad_existentente。我的推理是,每次迭代都会查询我的数据库,从而带回我想要的结果,但不是。我在做什么错了,我该怎么办
更新:
我已经对查询进行了一些更改,现在它以我想要的方式(多种)进行了工作。
更新了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})
现在的问题是如何在模板上显示元素,如何遍历列表以在查询集中获取项目?
终端结果
--->>>>>>21<<<<<<<---CANTIDAD EXISTENTE
--->>>>>>20<<<<<<<---CANTIDAD MINIMA
--->>>>>><QuerySet []><<<<<<<---
--->>>>>>4<<<<<<<---CANTIDAD EXISTENTE
--->>>>>>5<<<<<<<---CANTIDAD MINIMA
--->>>>>><QuerySet [<Item: Cortador Carburo 1/8>]><<<<<<<---
UPDAtE 2:添加模型
我使用的 Item 具有我清单中所有工具的所有公共字段,并且该类被具有特定工具唯一字段的其他类继承。
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%}
答案 0 :(得分:1)
由于您是将列表作为上下文发送的,因此您可以遍历模板中的列表
{% for item in items %}
<tr>
<td>{{ item.0 }}</td>
<td>{{ item.1 }}</td>
...
</tr>
{% endfor %}
如果这样不起作用,请尝试在索引之后添加字段名称,例如{{ item.0.description }}
答案 1 :(得分:0)
尝试
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})