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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:24:06+00:00 2026-05-26T06:24:06+00:00

I have Project and Tag models in my application with a many-to-many relationship between

  • 0

I have Project and Tag models in my application with a many-to-many relationship between them. On each project’s page I want to list 3 additional projects that have the most tags in common with it. How can I perform this query?

class Tag(models.Model):
  name = models.CharField(max_length=300)

class Project(models.Model):
  name = models.CharField(max_length=300)
  ...
  tags = models.ManyToManyField(Tag)
  • 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-26T06:24:06+00:00Added an answer on May 26, 2026 at 6:24 am

    It is possible to pack it all in one line:

    Project.objects.filter(tags__in=current_project.tags.all()).annotate(Count('name')).order_by('-name__count')[:3]
    

    Or, broken down in steps:

    tags = current_project.tags.all()
    matches = Project.objects.filter(tags__in=tags).annotate(Count('name'))
    results = matches.order_by('-name__count')[:3]
    

    The logic goes as follows:

    1. current_project is the instance of the project you want the relations for.
    2. The filter selects all projects that have tags that are the same as the current project.
    3. The annotate adds a variable to the return values that counts the number of similar names. As projects that match multiple tags are returned multiple times, this value in indicative for the number of matches.
    4. The results are sorted on the annotated name__count variable. To get the top 3 results, the list is capped using [:3].
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have Project , that is in many-to-many association with Tag . I'm
I have a has_many and belongs_to association set up between two models: Project and
so i have got two models with each one controller: Model Project has_many Themes
I have a following situation in my project. There are some tag values which
I have a Django project for managing a list of journal articles. The main
How I can identify what svn branch/tag points my code? I have project that
I have a 'project' model. Each project has a 'gallery' and each gallery has
I have a situation where Project can have many Tags (and vice versa), so
I have a django project with 2 apps like this: ## tags app, models.py
Currently I'm working on a RFID project where each tag is attached to an

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.