从模型中获取数据的问题

[英]issues with fetching data from models


One of my model is abstract model, which is inherited by another model.

我的一个模型是抽象模型,它由另一个模型继承。

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

inherited model

继承的模式

class OrderItem(SelectedProduct):

    order = models.ForeignKey("Order", related_name="items")

Now if I run this command in my views orderItem=OrderItem.objects.all() and in my templated {{passed_variable}} it returns

现在,如果我在我的视图中运行此命令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)...']

[ ,'...(其余元素被截断)......']

means no id, description etc. and if I specify {{passed_variable.id}} in my template then it shows nothing.

意味着没有id,描述等,如果我在模板中指定{{passed_variable.id}},那么它什么都不显示。

1 个解决方案

#1


0  

orderItem=OrderItem.objects.all() returns a list(queryset) of items

orderItem = OrderItem.objects.all()返回项目的列表(查询集)

You need to loop over the items

你需要遍历项目

in python:

在python中:

for one_order_item in orderItem:
    print one_order_item.id

in django templates:

在django模板中:

{% for one_order_item in passed_variable %}
       {{ one_order_iten.id }}
{% endfor %}
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/12/22/a667a269b1be63173403639e38376d0e.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告