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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:50:08+00:00 2026-05-11T02:50:08+00:00

Given a situation in Django 1.0 where you have extra data on a Many-to-Many

  • 0

Given a situation in Django 1.0 where you have extra data on a Many-to-Many relationship:

class Player(models.Model):   name = models.CharField(max_length=80)  class Team(models.Model):   name = models.CharField(max_length=40)   players = models.ManyToManyField(Player, through='TeamPlayer', related_name='teams')  class TeamPlayer(models.Model):   player = models.ForeignKey(Player)   team = models.ForeignKey(Team)   captain = models.BooleanField() 

The many-to-many relationship allows you to access the related data using attributes (the ‘players’ attribute on the Team object or using the ‘teams’ attribute on the Player object by way of its related name). When one of the objects is placed into a context for a template (e.g. a Team placed into a Context for rendering a template that generates the Team’s roster), the related objects can be accessed (i.e. the players on the teams), but how can the extra data (e.g. ‘captain’) be accessed along with the related objects from the object in context (e.g.the Team) without adding additional data into the context?

I know it is possible to query directly against the intermediary table to get the extra data. For example:

TeamPlayer.objects.get(player=790, team=168).captain 

Or:

for x in TeamPlayer.objects.filter(team=168):   if x.captain:     print '%s (Captain)' % (x.player.name)   else:     print x.player.name 

Doing this directly on the intermediary table, however requires me to place additional data in a template’s context (the result of the query on TeamPlayer) which I am trying to avoid if such a thing is possible.

  • 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. 2026-05-11T02:50:09+00:00Added an answer on May 11, 2026 at 2:50 am

    So, 15 minutes after asking the question, and I found my own answer.

    Using dir(Team), I can see another generated attribute named teamplayer_set (it also exists on Player).

    t = Team.objects.get(pk=168) for x in t.teamplayer_set.all():   if x.captain:     print '%s (Captain)' % (x.player.name)   else:     print x.player.name 

    Not sure how I would customize that generated related_name, but at least I know I can get to the data from the template without adding additional query results into the context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I do not have a schema to drop at hand,… May 11, 2026 at 12:33 pm
  • added an answer There is nothing wrong with your installation or environment. The… May 11, 2026 at 12:33 pm
  • added an answer It isn't super clean, but this will do it. The… May 11, 2026 at 12:33 pm

Related Questions

Given a situation where you have 2 projects that in total will provide enough
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a Python object of any kind, is there an easy way to get
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
I'm using Django to write a blog app, and I'm trying to implement a
I have a situation where in a web application a user may need a
Given a variable foo of type FooClass* and a member variable in that class

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.