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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:52:14+00:00 2026-05-11T07:52:14+00:00

I query a model: Members.objects.all() And it returns: Eric, Salesman, X-Shop Freddie, Manager, X2-Shop

  • 0

I query a model:

Members.objects.all() 

And it returns:

Eric, Salesman, X-Shop Freddie, Manager, X2-Shop Teddy, Salesman, X2-Shop Sean, Manager, X2-Shop 

What I want is to know the best Django way to fire a group_by query to my database, like:

Members.objects.all().group_by('designation') 

Which doesn’t work, of course. I know we can do some tricks on django/db/models/query.py, but I am just curious to know how to do it without patching.

  • 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. 2026-05-11T07:52:14+00:00Added an answer on May 11, 2026 at 7:52 am

    If you mean to do aggregation you can use the aggregation features of the ORM:

    from django.db.models import Count result = (Members.objects     .values('designation')     .annotate(dcount=Count('designation'))     .order_by() ) 

    This results in a query similar to

    SELECT designation, COUNT(designation) AS dcount FROM members GROUP BY designation 

    and the output would be of the form

    [{'designation': 'Salesman', 'dcount': 2},   {'designation': 'Manager', 'dcount': 2}] 

    If you don’t include the order_by(), you may get incorrect results if the default sorting is not what you expect.

    If you want to include multiple fields in the results, just add them as arguments to values, for example:

        .values('designation', 'first_name', 'last_name') 

    References:

    • Django documentation: values(), annotate(), and Count
    • Django documentation: Aggregation, and in particular the section entitled Interaction with default ordering or order_by()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Query model: class Query < ActiveRecord::Base belongs_to :test end which is
I have this query in model: public function Games() { $q = $this->db->select('games.id, games.title,
I run script/generate model query edit query.rb in models.. class Query < ActiveRecord::Base #I
Trying to query a 'Favorites' model to get a list of items a user
I have my mongoid query in the model def self.get_result collection_name, hash_params, page, per_page
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
Is it possible to query data from a Model without writing sqarql-queries? Adding properties
I would like to write a query in a Rails model using mongoid, and
I'm using an ADO.NET Entity Model which I'm trying to query using LINQ. The
I'm having an issue with my linq query. I am trying to filter objects

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.