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 8563947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:05:14+00:00 2026-06-11T17:05:14+00:00

Having the following model: from django_hstore import hstore from django.db import models class Item(VoteModel):

  • 0

Having the following model:

from django_hstore import hstore
from django.db import models

class Item(VoteModel):
    data = hstore.DictionaryField(db_index=True)
    objects = hstore.HStoreManager()

Something like:

Item.objects.extra(select={"key": "content_item.data -> 'key'"}).aggregate(Count('key'))

doesn’t work, cf. Using .aggregate() on a value introduced using .extra(select={…}) in a Django Query? and https://code.djangoproject.com/ticket/11671.

The raw SQL which works is the following:

SELECT content_item.data -> 'key' AS key, count(*)  FROM content_item GROUP BY key;                                                                               
   key     | count 
-----------+-------
 value1    |   223
 value2    |    28
 value3    |    31
(3 rows)

How can I get hte same results through Django’s ORM ?

FYI:

Item.objects.extra(select={"key": "content_item.data -> 'key'"})

translates to:

SELECT (content_item.data -> 'key') AS "key", "content_item"."id", "content_item"."data" FROM "content_item"
  • 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-06-11T17:05:15+00:00Added an answer on June 11, 2026 at 5:05 pm

    Have you tried with values and order_by?

    Item.objects.extra(
        select=dict(key = "content_item.data -> 'key'")
    ).values('key').order_by('key').annotate(total=Count('key'))
    

    Something like this works for me in PostgreSQL and Django 1.4.

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

Sidebar

Related Questions

Let's assume we have following models: from django.db import models class Foo(models.Model): name =
What's the best way to implement the following: ### models.py >>> from django.db import
Given my model is following: class Author(models.Model): name = models.CharField(max_length=100) description = models.TextField() class
Say I have the following in my models.py : class Company(models.Model): name = ...
I am having following class hierarchy - //Abstract Class1 in library1 (Can't modify this)
I am having following code for getting image from the web: NSURL *ImageURL =
suppose we have a model in django defined as follows: class Literal: name =
I have the following model Person in my UI MVC layer: public class Person
Having defined a Model public class HomeModel { [Required] [Display(Name = First Name)] public
I am using the following code to initialize a model from within my controller:

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.