我是一名新的Django程序员,正在编写一个学习应用程序。该应用程序具有外键关系,我使用prefetch_related方法。该系统运行良好;-)
我的问题是在模板上,当我显示表中的数据时,浏览器显示了这种提示:
ID帐单号仓库商品数量价格(<<-列) 1 CabeceraDeFacturas对象(1)底漆almacénBolsaplástico.05毫米。 KK。 10 1 ...
问题是: ¿为什么显示消息“ CabeceraDeFacturas对象(1)”而不是帐单号? ¿我可以解决这个问题吗? ¿如何?
有关的信息是:
models.py的一部分
class CabeceraDeFacturas(models.Model):
ID_cliente = models.ForeignKey('Clientes',on_delete=models.CASCADE)
fecha_factura = models.DateField('Fecha de factura: ')
ID_formaDePago = models.ForeignKey('FormasDePago',on_delete=models.CASCADE)
Porcentaje_IVA = models.PositiveSmallIntegerField(default=21)
Total_factura = models.IntegerField()
class Meta:
ordering = ('id',)
#def __str__(self):
# return self.id
class LineasDeFacturacion(models.Model):
ID_factura = models.ForeignKey('CabeceraDeFacturas',on_delete=models.CASCADE)
ID_almacen = models.ForeignKey('Almacen',on_delete=models.CASCADE)
ID_articulo = models.ForeignKey('Articulos',on_delete=models.CASCADE)
cantidad = models.IntegerField(default=1)
Precio = models.IntegerField()
class Meta:
default_related_name = 'lineas'
# def __str__(self):
# return str(self.ID_factura)
部分views.py
class VerFacturaCompleta(ListView):
#model = CabeceraDeFacturas
model = LineasDeFacturacion
template_name = "ListarTodasLasFacturasConLineas.html"
# recuperar las facturas completas y líneas --> comprobar
# queryset = CabeceraDeFacturas.objects.all().select_related('lineas')
queryset = LineasDeFacturacion.objects.all().prefetch_related('ID_factura')
paginate_by = 10
最后,模板:
<h1>Listar facturas</h1>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Factura</th>
<th scope ="col">Almacén</th>
<th scope ="col">Artículo</th>
<th scope ="col">Cantidad</th>
<th scope="col">Precio</th>
<!--<th scope="col">Almacén</th>-->
</tr>
</thead>
<tbody>
{% for articulo in object_list %}
<tr>
<!--<th scope="row"><a href= "{{ articulo.pk }}">{{ articulo.pk }}</a></th>-->
<th scope ="row"> {{ articulo.pk }} </th>
<td>{{ articulo.ID_factura }}</td>
<td>{{ articulo.ID_almacen }}</td>
<td>{{ articulo.ID_articulo }}</td>
<td>{{ articulo.cantidad }}</td>
<td>{{ articulo.Precio }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<hr>
非常感谢!
答案 0 :(得分:0)
好吧,模板仅使用“ object_list”。该object_list来自这样的指令:“ queryset = LineasDeFacturacion.objects.all()。prefetch_related('ID_factura')”;在视图中。
下一个用于此模板的视图是
class VerFacturaCompleta(ListView):
#model = CabeceraDeFacturas
model = LineasDeFacturacion
template_name = "ListarTodasLasFacturasConLineas.html"
# recuperar las facturas completas y líneas --> comprobar
# queryset = CabeceraDeFacturas.objects.all().select_related('lineas')
queryset = LineasDeFacturacion.objects.all().prefetch_related('ID_factura')
paginate_by = 10
仅在视图内部不使用上下文,视图将object_list(查询集)传递给模板。
感谢您的时间!
答案 1 :(得分:0)
我发现了错误。问题出在models.py声明中。 CabeceraDeFactura类没有数据返回。当我修复此错误
def __str__(self):
return str(self.id)
模板正确显示数据。
感谢您的时间!