我的一个模型是抽象模型,它由另一个模型继承。
class SelectedProduct(models.Model):
sku = fields.SKUField()
description = CharField(_("Description"), max_length=200)
quantity = models.IntegerField(_("Quantity"), default=0)
unit_price = fields.MoneyField(_("Unit price"), default=Decimal("0"))
total_price = fields.MoneyField(_("Total price"), default=Decimal("0"))
class Meta:
abstract = True
继承模型
class OrderItem(SelectedProduct):
order = models.ForeignKey("Order", related_name="items")
现在,如果我在我的观看orderItem=OrderItem.objects.all()
和我的模板{{passed_variable}}
中运行此命令,则会返回
[<OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, <OrderItem: >, '...(remaining elements truncated)...']
表示没有ID,描述等,如果我在模板中指定{{passed_variable.id}}
,则表示没有显示任何内容。
答案 0 :(得分:0)
orderItem=OrderItem.objects.all()
返回项目列表(queryset)
你需要遍历项目
在python中:
for one_order_item in orderItem:
print one_order_item.id
在django模板中:
{% for one_order_item in passed_variable %}
{{ one_order_iten.id }}
{% endfor %}