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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:35:08+00:00 2026-06-12T15:35:08+00:00

I have a problem that I would like to solve using Django’s ORM. I

  • 0

I have a problem that I would like to solve using Django’s ORM. I have the following classes:

class Company:
    name = models.CharField(max_length=200)

class Event:
    title = models.CharField(max_length=200)
    company = models.ForeignKey(Company)

class Registration:
    attended = models.BooleanField(default=False)
    event = models.ForeignKey(Event)

These are simple examples, but basically a Company can have several Events. When a user registers to attend an Event, a Registration is created for that user (I know that field isn’t there, that’s not the issue). If the user actually shows up to the Event, then that user’s Registration object will have “attended=True”.

I’m currently generating reports, and what I need is essentially a way to have a Company see all of their Events, and sort by the number of attendees (Registrations with “attended=True”).

I have a SQL statement that may give you an idea of what I need (except that instead of just “e.title”, obviously a QuerySet would give me back the whole object):

SELECT e.title FROM example_event e LEFT JOIN (SELECT event_id,
COUNT(*) attendees FROM example_registration WHERE attended=1 GROUP BY
event_id) r ON (e.id=r.event_id) ORDER BY r.attendees DESC;

That gives me a properly sorted result set of Event titles. But I’d really like to do this within the ORM and return a QuerySet. Is there any way to do this? I would basically need something like:

events.annotate(attendees=Count('registration__attended=True'))
                .order_by('attendees')

… I know that statement is invalid, but it hopefully gets the point across of what I’m trying to accomplish. Also, I need to always return all of the Events for a Company, because it’s a report. They will want to see how all of their Events are performing, even if they have 0 attendees (so something like events.filter(registration__attended=True) won’t give me what I need, because it returns only a subset of a Company’s Events).

Thanks in advance, I’d really appreciate someone’s help here!

  • 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-12T15:35:09+00:00Added an answer on June 12, 2026 at 3:35 pm

    I’m not sure if you want to exclude use of the extra method but you could do the following:

    events = Event.objects.extra(
        select={'attendees': 'SELECT COUNT(*) from event_registration' + \
            ' where attended=1 and event_id=event_event.id'}
    ).order_by('attendees')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting genetics problem that I would like to solve in native
i have the following problem: i would like to create a footer. that footer
My problem is: I have 3 procs that would like to share config loaded
Problem: I have a table that prints out vertical but I would like it
i have a timestamp problem. i would like to check a timestamp if that
I would like to solve/implement the 8 puzzle problem using the A* algorithm in
I have a problem that is kind of difficult to explain. What I would
I experience strange problem. We have error handling in global.asax that would redirect user
I found this question, which is similar to a problem that I would like
I have a particular problem that I can't seem to solve. Is it possible

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.