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

The Archive Base Latest Questions

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

I have a situation like this: there are three Django models, let’s call them

  • 0

I have a situation like this: there are three Django models, let’s call them article, section and tag, and one through-model.

class Tag(models.Model):
    name = models.CharField(max_length=100, primary_key=True)

class Section(models.Model):
    # all sorts of fields here

class Article(models.Model):
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    sections = models.ManyToManyField(Section, null=True, blank=True, through='SectionInArticle', related_name='articles')

class SectionInArticle(models.Model):
    article = models.ForeignKey(Article)
    section = models.ForeignKey(Section)
    order = models.IntegerField()

Then, in the section’s detail template, I want to list all the tags from the related articles. To do that, I first have to traverse the Section-Article ManyToMany relationship in reverse (using the related_name), and then traverse the Article-Tag ManyToMany relationship. I tried this:

{# doesn't print anything: #}
{% for tag in object.articles.tags.all %}
{{ tag.name }}
{% endfor %}

but for some reason, that didn’t work. {% for tag in object.articles.all.tags.all %} didn’t work either. I can print all the tags using nested loops, but that means duplicates become a problem.

{# duplicates are an issue here: #}
{% for article in object.articles.all %}
  {% for tag in article.tags.all %}
    {{ tag.name }}
  {% endfor %}
{% endfor %}

Is there a neat way to do this in Django templates, or do I have to put it in the view code? If so, what would be the be the cleanest way to do it there so I can avoid duplicate tags in the list?

  • 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-20T14:06:13+00:00Added an answer on May 20, 2026 at 2:06 pm

    The filter you are looking for is basically:

    Tag.objects.filter(article__sections=section)
    

    You can put it the your view, or you could for add it to your Section model as a property:

    class Section(models.Model):
    # all sorts of fields here
    @property
    def tags(self):
        return Tag.objects.filter(article__sections=self).all()
    

    And then in the template do:

    {% for tag in object.tags %}
        {{ tag.name }}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a situation where you have something like a Lite, Normal, Ultimate tiers and
I have a problem that seems like its a result of a deadlock-situation. Whe
I have a situation where I have two models, companies and permissions, where companies
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I'm wondering if there is anything like Django's HTML templating system, for for CSS..
I have a situation like below in which I have some problem in freeing
Here's the situation: I have three files, Test1.cpp and Test2.cpp. Test1.cpp can be compiled
We have a situation where users are allowed to upload content, and then separately
We have a situation in our product where for a long time some data
I have a situation where I might have multiple instances of a program running

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.