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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:46:35+00:00 2026-05-13T06:46:35+00:00

This question relates to Django Aggregation/Annotation in 1.1. Suppose I have a simple model

  • 0

This question relates to Django Aggregation/Annotation in 1.1. Suppose I have a simple model with an IntegerField that has a “choices” parameter passed to it. In this case, it maps to a GENDERS tuple as shown. Normally, in a template (or view) I can refer to the textual value of the gender by using the get_gender_display() function.

However, when I annotate the gender counts as I do in the view below, I can’t then lookup the textual value for each gender using the get_gender_display() function. See the template excerpt below. How would I go about getting the textual value for gender back?

I’ve never used Django Aggregation before, so maybe I’m missing an obvious solution. Thank you for considering my situation.

models.py

GENDERS = (
  ('','    '),
  (1,'Female'),
  (2,'Male'),
)

class Subscriber(models.Model):
   gender = models.IntegerField(blank=True, null=True, choices=GENDERS)

views.py

from django.db.models import Count

def myview(request):
... 
    sum_gender = Subscriber.objects.values('gender').annotate(gender_sum=Count('gender')) 
    context = { 'sum_gender':sum_gender, }
    return render_to_response(template_name, context,context_instance=RequestContext(request)) 

template

...
{% for genderAndsum in sum_genders %}
  <div>{{ genderAndsum.get_gender_display }} {{ genderAndsum.gender_sum }}</div>
{% endfor %} 
...
  • 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-13T06:46:35+00:00Added an answer on May 13, 2026 at 6:46 am

    The problem is not the aggregation, it’s that you’re using values(). This returns a ValuesQuerySet, each of whose entries is a dictionary – not a Subscriber object. Obviously, that dict doesn’t have any get_FOO_display methods.

    The quickest solution would be to use a custom filter:

    from myapp.models import GENDERS
    
    @register.filter
    def gender_lookup(value)
        gender_dict = dict(GENDERS)
        return gender_dict.get(value, '')
    

    and in the template:

    <div>{{ genderAndsum.gender|gender_lookup }} {{ genderAndsum.gender_sum }}</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question relates to this question . I have a EF model like this
This relates to my other question on accessing a REST service that uses forms
I have some Django models with a structure similar to this: class Grandparent(models.Model): name
I have a simple hierarchic model whit a Person and RunningScore as child. this
Suppose I have this model: class PhotoAlbum(models.Model): title = models.CharField(max_length=128) author = models.CharField(max_length=128) class
this question relates to performance penalities that may or may not arise from having
This question assumes that the python package I want to install is a django
This question has been retitled/retagged so that others may more easily find the solution
this question has been answered several times and i have seen almost all related
I have a Django project that has two models: Group and Person. Groups can

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.