How could create a crosstab SQL query with Django ORM? -


Using Django 1.1, how can I create a crosstab (pivot table) SQL query using ORM?

Updated: These models and output requirements are:

  class store (models.Model): name = models.CharField (max_length = 255) ... class order (Models.Model): store = models.ForeignKey (store, empty = true, null = true, related_name = 'command') description = models.CharField (_ ('description'), MAX_LENGTH = 255) quantity = models.IntegerField (Empty = true, null = true) type_detail = models.CharField (_ ('type in detail'), MAX_LENGTH = 255) slug = models.SlugField (empty = true) cost = models.DecimalField (_ ("cost") , Max_digits = 14, decimal_places = 2) Modified = models.DateTimeField (_ ('modified'), auto_now = true)  

currently this view If showing data like this:

  Store | Type extension | Volume ---------------------------------- Walmart | Floor polish | Walmart 2 Tiles | 1 Walmart | Milk | 4 another. Floor polish | 2 another. Tiles | 1 another. Milk | 4  

I want to smoke it to see the data like this:

I should know the quantity for a shop

  Store | Floor polish | Tiles | Milk ------------------------------------------------ Walmart | 2 | 1 | 4 another. 2 | 1 | 4  

I hope that tells me what I want.

You can do this in the template as follows (assuming you passing your template line_items And it's also a unique asset to handle store.name):

  {% line_items store.name again collected as store_items%} {store_items%} store in For%} & lt; Tr & gt; & Lt; Td> {{Store.grouper}} & lt; / Td> In {% Store.list%} & lt; Td> {{Item.count}} & lt; / Td> {% Endfor%} & lt; / Tr & gt; {% Endfor%}  

This is the only list of all stores, otherwise you will need to fill the gap in view (for example, returning to 0 for the list items unavailable), this Will work


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -