Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:11:27+00:00 2026-05-13T08:11:27+00:00

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

  • 0

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

UPDATED:
These are the models and output requirements:

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)

Currently the view is showing data like so:

Store   | Type Detail  | Quantity 
----------------------------------
Walmart | Floor polish | 2        
Walmart | Tiles        | 1        
Walmart | Milk         | 4      
Another | Floor polish | 2        
Another | Tiles        | 1        
Another | Milk         | 4        

I want to pivot this to view the data like so:

For a store I need to know quantities

Store   | Floor polish  | Tiles | Milk
------------------------------------------------
Walmart | 2             | 1     | 4
Another | 2             | 1     | 4

I hope that explains what I need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:11:28+00:00Added an answer on May 13, 2026 at 8:11 am

    You can do this in the template as follows (assuming you are passing line_items to your template and also assuming store.name is a unique property):

    {% 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 %}
    

    This would work if all stores have the same inventory, otherwise you’ll need to fill the gaps in the view (returning 0’s for missing inventory items, for example)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use mod_proxy (or mod_proxy_balancer) to forward requests on… May 13, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer Keys key = Keys.b | Keys.c; var lowest = Enum.GetValues(typeof(Keys))… May 13, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer Define the function on the window object to make it… May 13, 2026 at 9:20 pm

Related Questions

I need to import some data from a excel file and a folder with
I'm a PHP guy on my first day in Python-land, trying to convert a
How do I install a Django snippet, in particular snippet 818 - dumpscript ,
I'm writing a simple CMS based on Django. Most content management systems rely on
I would like to be able to dynamically retrieve the current executing module or

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.