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

  • Home
  • SEARCH
  • 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 8230223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:03:46+00:00 2026-06-07T17:03:46+00:00

When a ManyToMany relationship has extra data via a through table, how can you

  • 0

When a ManyToMany relationship has extra data via a through table, how can you get to the data in a template? From a view I can get the data if I supply parameters:

class Category(models.Model):
    title           = models.CharField(max_length=1024,null=True,blank=True)
    entry           = models.ManyToManyField(Entry,null=True,blank=True,
                                             related_name='category_entry',
                                             through='CategoryEntry',
                                             )

class CategoryEntry(models.Model):
    category    = models.ForeignKey(Category)
    entry       = models.ForeignKey(Entry)
    votes       = models.IntegerField(null=False, default=0)

def category_detail(request, pk):
    category = models.Category.objects.select_related().get(pk=pk)
    entries  = category.entry.order_by('-temp_sort_order').filter(temp_sort_order__gte=0)
    for entry in entries:
        assert isinstance(entry, models.Entry)
        ce = models.CategoryEntry.objects.get(entry=entry, category=category)
        pprint('Show votes as a test: ' + ce.votes) #OK
        pprint('entry title: ' + entry.title) #OK
        pprint('entry votes: ' + str(entry.category_entry.votes)) #BAD
        pprint('entry votes: ' + str(entry.entry.votes))  #BAD
    ....

But templates can’t supply parameters to methods.

The documentation at https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships is silent on templates. Using using for entry in category.category_entry_set.all gives ‘Category’ object has no attribute ‘category_entry_set’. category.category_entry.all does not work either.

Ultimately I want to display the extra data in a template:

{% for entry in entries %}
    <ul>
        <li>Title: {{ entry.title }} Votes: {{ entry.category_entry.votes }} {{ entry.entry.votes }}</li>
    </ul>
{% endfor %}
  • 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-07T17:03:48+00:00Added an answer on June 7, 2026 at 5:03 pm

    If you have a category instance in template:

    category.entry.all -> list of entries
    

    If you have an entry instance in template:

    entry.category_entry.all -> list of categories
    

    You should call M2M fields in plural form,
    then you will have a more readable code

    category.entries.all
    

    %model%_set syntax (or related name, if you’ve specified it) is using to access to model trough a backward relationship.

    https://docs.djangoproject.com/en/1.4/topics/db/queries/#following-relationships-backward

    But how do I get the ‘votes’ associated with the m2m instance? – Bryce

    I suggest you the following way:

    class Category(models.Model):
        title           = models.CharField(max_length=1024,null=True,blank=True)
        entries           = models.ManyToManyField(Entry,null=True,blank=True,
                                                 related_name='categories',
                                                 through='CategoryEntry',
                                                 )
    
    class CategoryEntry(models.Model):
        category    = models.ForeignKey(Category, related_name='category_entries')
        entry       = models.ForeignKey(Entry)
        votes       = models.IntegerField(null=False, default=0)
    
    def category_detail(request, pk):
        category = models.Category.objects.select_related().get(pk=pk)
        category_entries  = category.category_entries.filter(entry__temp_sort_order__gte=0).order_by('-entry__temp_sort_order')
        for category_entry in category_entries:
            # category_entry is an instance of the model CategoryEntry
            pprint('category entry votes: ' + str(category_entry.votes))
            pprint('entry title: ' + category_entry.entry.title)
       ....
    
    HOW TO
    entry = Entry.objects.get(pk=1)
    entry.categories.all() # list of categories (here we work through related name of the field entries)
    
    category = Category.objects.get(pk=1)
    category.entries.all() # list of entries (here we work through m2m field entries)
    
    category.category_entries.all() # list of CategoryEntry objects (through related name category_entries of the field category in model CategoryEntry)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that has a manytomany relationship with a 'through' table in
from an instance of Site with a ManyToMany relationship to Kiosk, i'd like to
If my models.py has a ManyToMany relationship between books and authors, and if for
I have two objects that have a ManyToMany relationship with one another through a
I have an entity that has a ManyToMany relationship with itself: @Entity public class
I have an Entity Account which has a ManyToMany Relationship to an Entity Role.
I have ManyToMany relationship between Author and Book . table : AUTHOR id |
I have two classes with a ManyToMany relationship. I'd like to select one from
Researching I found that javax.persistence.ManyToMany relationship can be implemented various collection. I wonder if
I have 2 entities. Mag Page Which have a ManyToMany relationship. A Page can

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.