KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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
Tags (comma-separated)
Save Edits
Cancel