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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:27:06+00:00 2026-05-20T17:27:06+00:00

I have a model with articles, events and people: class Person(models.Model): id = models.IntegerField(primary_key=True)

  • 0

I have a model with articles, events and people:

class Person(models.Model):
  id = models.IntegerField(primary_key=True)
  name = models.CharField(max_length=30)

class Event(models.Model):
  id = models.IntegerField(primary_key=True)
  title = models.CharField(max_length=200)

class Article(models.Model):
  id = models.IntegerField(primary_key=True)
  title = models.CharField(max_length=200)
  publishDate = models.DateTimeField(blank=True, null=True)
  event = models.ForeignKey(Event, blank=False, null=False)
  persons = models.ManyToManyField(Person)

An article belongs to an event, and events consist of many articles. Persons appear in many articles and articles contain many persons. The idea is to see which events were most written about inside a given time interval. I did this in a single query:

topEvents = Article.objects.filter(publishDate__gt=dateStart)
                           .filter(publishDate__lt=dateEnd)
                           .values('event').annotate(count=Count('id'))
                           .order_by('-count')[:topN]

I found that this takes significantly less time than calculating the top N server-side.

Now, the question is, how do I do the same with the ManyToMany relation I have with the Persons? Also, is this the best way to do this?

  • 1 1 Answer
  • 2 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-20T17:27:07+00:00Added an answer on May 20, 2026 at 5:27 pm

    To simplify your example:

    class Event(models.Model):
        title = models.CharField(max_length=200)
    
    class Person(models.Model):
        name = models.CharField(max_length=30)
    
    class Article(models.Model):
        title = models.CharField(max_length=200)
        publishDate = models.DateTimeField(blank=True, null=True)
        event = models.ForeignKey(Event, related_name='articles')
        persons = models.ManyToManyField(Person, related_name='articles')
    

    Note that there are no id fields as Django creates them automatically. Also, both event and persons on the Article class have related_name set to ‘articles’ which allows us to refer to articles simply by using event.articles and person.articles.

    Now, to get the events and people that are written about most often, you can query directly on their model managers:

    Event.objects.filter(articles__publishDate__gt=dateStart)\
                 .filter(articles__publishDate__lt=dateEnd)\
                 .annotate(count=Count('articles')).order_by('-count')
    
    Person.objects.filter(articles__publishDate__gt=dateStart)\
                  .filter(articles__publishDate__lt=dateEnd)\
                  .annotate(count=Count('articles')).order_by('-count')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using polymorphic association. I have 2 models articles and events which have
I have a model Articles that has_many Assets which is a polymorphic model that
I have a controller called articles , which creates the articles model which gets
I have two model: User, Article A user can like or dislike many articles,
I have the following listview : http://www.vogella.de/articles/AndroidListView/article.html the chaper: 8. Tutorial: Domain Model and
I have a following model: abstract Article {id, name, text} final NewspaperArticle { printDate
I have some models; Vocabularies (tags lists), Labels (tags) and different articles types. These
I'm aware of the different event models in JavaScript (the WC3 model versus the
I have built a base class for my view model(s). Here is some of
I have a Model called Section which has many articles ( Article ). These

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.