如何使用Django 1.1 ORM创建交叉表(数据透视表)SQL查询?
更新: 以下是模型和输出要求:
class Store(models.Model):
name = models.CharField(max_length=255)
...
class Order(models.Model):
store = models.ForeignKey(Store, blank=True, null=True, related_name='orders')
description = models.CharField(_('Description'), max_length=255)
quantity = models.IntegerField(blank=True, null=True)
type_detail = models.CharField(_('Type Detail'), max_length=255)
slug = models.SlugField(blank=True)
cost = models.DecimalField(_("Cost"), max_digits=14, decimal_places=2)
modified = models.DateTimeField(_('modified'), auto_now=True)
目前视图显示的数据如下:
Store | Type Detail | Quantity
----------------------------------
Walmart | Floor polish | 2
Walmart | Tiles | 1
Walmart | Milk | 4
Another | Floor polish | 2
Another | Tiles | 1
Another | Milk | 4
我希望将其转换为以下形式以查看数据:
对于商店,我需要知道数量。
Store | Floor polish | Tiles | Milk
------------------------------------------------
Walmart | 2 | 1 | 4
Another | 2 | 1 | 4
I hope that explains what I need.