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

The Archive Base Latest Questions

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

I’m creating a Person Group and Membership as described in Django docs for intermediate

  • 0

I’m creating a Person Group and Membership as described in Django docs for intermediate model.

class Person(models.Model):
  name = models.CharField(max_length=128)

  def __unicode__(self):
    return self.name

class Group(models.Model):
  name = models.CharField(max_length=128)
  members = models.ManyToManyField(Person, through='Membership')

  def __unicode__(self):
    return self.name

class Membership(models.Model):
  person = models.ForeignKey(Person)
  group = models.ForeignKey(Group)
  date_joined = models.DateField()
  invite_reason = models.CharField(max_length=64)

It is possible to access the Person from a Group object with:

>>>Group.members.name

Does Django creates another query to fetch the Person?
Can I access the date_joined field from a Group object?

The thing that confuses me is that I would expect to get the Person name field with:

>>>Group.members.person.name

What happens if a Person has a field ‘name’ and also the intermediate model have a field ‘name’.

  • 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-13T08:20:46+00:00Added an answer on May 13, 2026 at 8:20 am

    The members field in your example is a ManyToManyField, so it’s a way to access many people rather than one person.
    The object that is under the members field is actually a special type of Manager, not a Person:

    >>> print my_group.members
    <django.db.models.fields.related.ManyRelatedManager object at 0x181f7d0>
    

    To understand better what a Manager is, see the documentation.

    To access a person’s name you would do for example:

    >>> for person in my_group.members.all():
    >>>     print person.name
    

    You cannot access the fields in your Membership model via the Manager in the members field. To access any of the fields in it you would do:

    >>> for membership in my_group.membership_set.all():
    >>>     print membership.date_joined
    

    And so if you had a field called name in your Membership model, you would access it like this:

    >>> for membership in my_group.membership_set.all():
    >>>     print membership.name
    

    A second way to access a Person’s name would be:

    >>> for membership in my_group.membership_set.all():
    >>>     print membership.person.name
    

    Note that membership_set is a default name for the manager pointing towards the membership, but it can be changed by specifying related_name in the corresponding foreign key. For example if the foreign key from the Membership to the Group would be defined like such:

        group = models.ForeignKey(Group, related_name="group_members")
    

    Then you would access the manager using group_members:

    >>> for membership in my_group.group_members.all():
    >>>     print membership.name
    

    Hope that helps a little 🙂

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

Sidebar

Related Questions

No related questions found

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.