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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:51:57+00:00 2026-05-29T16:51:57+00:00

I have two models in Django linked together by ManyToMany relation like this: class

  • 0

I have two models in Django linked together by ManyToMany relation like this:

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

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

I need to get the main person in the group which is the first person in the group. How can I get the first person?

Here’s how I’m adding members:

grp = Group.objects.create(name="Group 1")
grp.save()
prs = Person.objects.create(name="Tom")
grp.members.add(prs) #This is the main person of the group.
prs = Person.objects.create(name="Dick")
grp.members.add(prs)
prs = Person.objects.create(name="Harry")
grp.members.add(prs)

I don’t think I need any additional columns as the id of the table group_members is a running sequence right.

If I try to fetch the main member of the group through Group.objects.get(id=1).members[0] then Django says that the manager is not indexable.

If I try this Group.objects.get(id=1).members.all().order_by('id')[0], I get the member with the lowest id in the Person table.

How can I solve this?

Thanks

  • 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-29T16:51:58+00:00Added an answer on May 29, 2026 at 4:51 pm

    Django doesn’t pay attention to the order of the calls to add. The implicit table Django has for the relationship is something like the following if it was a Django model:

    class GroupMembers(models.Model):
        group = models.ForeignKey(Group)
        person = models.ForeignKey(Person)
    

    Obviously, there’s nothing there about an “order” or which you should come first. By default, it will probably do as you describe and return the lowest pk, but that’s just because it has nothing else to go off of.

    If you want to enforce an order, you’ll have to use a through table. Basically, instead of letting Django create that implicit model, you create it yourself. Then, you’ll add a field like order to dictate the order:

    class GroupMembers(models.Model):
        class Meta:
            ordering = ['order']
    
        group = models.ForeignKey(Group)
        person = models.ForeignKey(Person)
        order = models.PositiveIntegerField(default=0)
    

    Then, you tell Django to use this model for the relationship, instead:

    class GroupMembers(models.Model):
        group = models.ForeignKey(Group)
        person = models.ForeignKey(Person, through='GroupMembers')
    

    Now, when you add your members, you can’t use add anymore, because additional information is need to complete the relationship. Instead, you must use the through model:

    prs = Person.objects.create(name="Tom")
    GroupMembers.objects.create(person=prs, group=grp, order=1)
    prs = Person.objects.create(name="Dick")
    GroupMembers.objects.create(person=prs, group=grp, order=2)
    prs = Person.objects.create(name="Harry")
    GroupMembers.objects.create(person=prs, group=grp, order=3)
    

    Then, just use:

    Group.objects.get(id=1).members.all()[0]
    

    Alternatively, you could simply add a BooleanField to specify which is the main user:

    class GroupMembers(models.Model):
        group = models.ForeignKey(Group)
        person = models.ForeignKey(Person)
        is_main_user = models.BooleanField(default=False)
    

    Then, add “Tom” like:

    prs = Person.objects.create(name="Tom")
    GroupMembers.objects.create(person=prs, group=grp, is_main_user=True)
    

    And finally, retrieve “Tom” via:

    Group.objects.get(id=1).members.filter(is_main_user=True)[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
Django newbie here. I have two simple models like this: class Fluff(models.Model): # ....
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
If I have two models in Django: class Blog(models.Model): author = models.CharField() class Post(models.Model):
I have two models like this: class A(models.Model): attachment = FileField(upload_to='a') class B(models.Model): attachment
I have two models such that class JobTitle(models.Model): name = models.CharField(max_length=1000) class Employer(models.Model): jobtitle
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have two models like this: class Collar(models.Model): num_tags = models.BigIntegerField() class Dog(models.Model): num_legs

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.