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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:27:46+00:00 2026-05-29T09:27:46+00:00

I have a model Data , associated to a table like this (The model

  • 0

I have a model Data, associated to a table like this (The model Data is made up of only IntegerField):

subject | year | quarter | sales |
----------------------------------
   1    | 2010 |   1     | 20    |
   1    | 2010 |   2     | 100   |
   1    | 2010 |   3     | 100   |
   1    | 2010 |   4     | 20    |
   1    | 2011 |   1     | 30    |
   1    | 2011 |   2     | 50    |
   1    | 2011 |   4     | 40    |
   2    | 2010 |   1     | 30    |
   2    | 2010 |   2     | 20    |
 [..-GO ON this way...]

I want to have a django-admin table, in read-only having columns (current year = 2011, quarter = 1)

subject | sales current year | sales current quarter | sales last year | sales current quarter last year |
----------------------------------------------------------------------------------------------------------
  1     |  110               |  30                   |  240            |  20
[AND SO ON]

The question is: It is possible do that using django-admin? What’s the way out?

  • 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-29T09:27:47+00:00Added an answer on May 29, 2026 at 9:27 am

    You can use methods on your Model or your ModelAdmin as items for list_display. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

    Since these are methods that might be useful outside the admin, as well, I’d suggest adding them to your Model.

    from django.db.models import Sum
    
    class Data(models.Model):
        ...
    
        # Method used by `get_current_year_sales` and `get_last_year_sales`
        # to stay DRY. Not for use directly in admin.
        def get_year_sales(self, year):
            qs = self.model._default_manager.filter(year=year)
            sales_agg = qs.aggregate(Sum('sales'))
            return sales_agg['sales__sum']
    
        # Method used by `get_current_quarter_sales` and `get_last_quarter_sales`
        # to stay DRY. Not for use directly in admin.
        def get_quarter_sales(self, year, quarter):
            qs = self.model._default_manager.filter(year=year, quarter=quarter)
            sales_agg = qs.aggregate(Sum('sales'))
            return sales_agg['sales__sum']
    
        def get_current_year_sales(self):
            return self.get_year_sales(datetime.now().year)
        get_current_year_sales.short_description = 'Sales (Current Year)'
    
        def get_last_year_sales(self):
            return self.get_year_sales(datetime.now().year-1)
        get_last_year_sales.short_description = 'Sales (Last Year)'
    
        def get_current_quarter_sales(self):
            # Determine current quarter logic here as `current_quarter`
            # `quarter_year` will likely be same as current year here,
            # but will need to be calculated for previous quarter
            return self.get_quarter_sales(quarter_year, current_quarter)
        get_current_quarter_sales.short_description = 'Sales (Current Quarter)'
    
        def get_current_quarter_sales(self):
            # Logic here to determine last quarter as `last_quarter`
            # Logic to determine what year last quarter was in as `quarter_year`
            return self.get_quarter_sales(quarter_year, last_quarter)
        get_last_quarter_sales.short_description = 'Sales (Last Quarter)'
    

    The short_description attribute determines what the admin will show as the row header for these methods. So, once you have all this in place, you need only modify your ModelAdmin‘s list_display attribute like:

    class DataAdmin(admin.ModelAdmin):
        ...
        list_display = ('subject', 'get_current_year_sales', 'get_last_year_sales', 'get_current_quarter_sales', 'get_last_quarter_sales')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User controller and User model. This model and associated database table
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
I have made a data model, and now I've made a 2nd version. All
I have a model that looks like this: Ext.regModel('TreeItem', { fields: [ { name:
I have a table called informations (http://pastebin.com/h3UiUhpx) and also have this associated tables educations,
I have some data associated with a model that is in a hash. The
In my data model I have a fairly common division between my objects/tables/data: Transactional
In my core data model I have a Person entity that has a to
In my Core Data model I have two entities: List and Patient. List has
In a webapp, i have a data-model (let's call it Item). The data of

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.