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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:00:24+00:00 2026-05-23T08:00:24+00:00

I have to classes such that class JobTitle(models.Model): name = models.CharField(max_length=500,null=False,blank=False) class Employer(models.Model): name

  • 0

I have to classes such that

class JobTitle(models.Model):
    name = models.CharField(max_length=500,null=False,blank=False)


class Employer(models.Model):
     name = models.CharField(max_length=500,null=False,blank=False)
     jobtitle = models.ForeignKey(JobTitle,null=False)
     revenue = models.IntegerField(null=False)

First I would like to get first 5 job titles whose number of employers is maximum and secondly I need to evaluate those avarage revenue of employers of those five jobtitles.

I may get first 5 job titles like

jtList = JobTitle.objects.filter(isActive=True).annotate(employer_count=Count('employer')).order_by('employer_count')[:5]

I may go over each jobtitle and their employers and sum their revenues and divide them the total number but of course it’s a bad and inefficient solution.

How can I achive such operation in django efficiently ?

Thank you

JobTitle.objects.annotate(jobtitle_count=Count('jobtitle')).order_by('-jobtitle_count')[:5]

  • 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-23T08:00:25+00:00Added an answer on May 23, 2026 at 8:00 am

    Summing Revenues in the same currency

    If all the revenue numbers are in the same currency, you can do this using annotate and Avg.

    job_titles = JobTitle.objects.filter(isActive=True
                                  ).annotate(employer__count=Count('employer'), 
                                             average_revenue=Avg('employer__revenue'),
                                  ).order_by('employer__count')[:5]
    

    Summing Revenues in different currencies

    In the comments you mention you’d like to convert revenues from GBP and EUR then sum in USD. I don’t have an elegant solution to this I’m afraid.

    One approach is to use values() to get the sums in each currency:

    # look at the employers in the top job titles
    employers = Employer.objects.filter(job_title=job_titles)
    currency_totals = employers.values('job_title', 'currency', Count('revenue'), Sum('revenue'))
    

    This will return a ValuesQuerySet, for example

    [{'job_title':1, 'currency': 'GBP', 'revenue__sum': 20.0, 'revenue__count': 1},
     {'job_title':1, 'currency': 'EUR', 'revenue__sum': 35.0, 'revenue__count': 2},
     {'job_title':2, 'currency': 'USD', 'revenue__sum': 50.0, 'revenue__count': 1},
     ...
    ]
    

    You can then loop through the results in the view, converting each revenue__sum to USD and adding to a running total for that job title.

    This way, you only need one query to get the revenue sums for a given set of job_titles. However, because we process the results in the view, you can’t get the database to filter on, or order by the revenue calculations e.g. “show me job titles with equivalent revenue greater than USD 1000”.

    I’d be interested to see any other approaches — we have customers paying us in GBP, EUR and USD, so we need to generate similar reports.

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

Sidebar

Related Questions

I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
If I have for example two classes A and B , such that class
Is it bad policy to have lots of work classes(such as Strategy classes), that
I have a few classes, such as those that outline database table structure or
I have 3 abstract classes (Customer, Tender, Site). For Customer has inherited class such
I have an abstract base class that many classes extend. I'd like all these
I have two classes defined such that they both contain references to the other
I have classes such as AccountsController, ProductsController etc that all inherit from BaseController. Unity
For example i have two classes A and B, such that for two objects
I have Three different classes that all need to use instances of one class.

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.