Django 1.1을 사용하면 어떻게 ORM을 사용하여 크로스 탭 (피벗 테이블) SQL 쿼리를 만들 수 있습니까? 업데이트]어떻게 Django 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
내가 원하는 것을 설명하기를 바랍니다. 모든 상점이 같은 경우
{% regroup line_items by store.name as store_items %}
{% for store in store_items %}
<tr>
<td>{{ store.grouper }}</td>
{% for item in store.list %}
<td>{{ item.count }}</td>
{% endfor %}
</tr>
{% endfor %}
이 작동합니다 :
당신은 다음과 같이 템플릿에서이 작업을 수행 할 수 있습니다
좀 더 자세하게 설명해야합니다. 어떤 모델을 가지고 있으며 어떤 결과를 성취하고 싶습니까? –
모델 세부 정보로 업데이트했습니다. –