Search code examples
djangomany-to-many

many to many ordered records django


I am getting all related records using user.companies.all().

But this query is giving me random records from the userProfile_companies table. I need the order in which the records are added in the bridging table. models.py

class UserProfile(models.Model):
    companies = models.ManyToManyField('Company', blank=True, null=True)

company_manager.py

for company in user.companies.all():
    companiesToBeIncluded.append(company.id)

Here I'm getting incorrect order of company ids. Thanks


Solution

  • Accessing the through table was the solution here. here's the post.

    UserProfile.companies.through.objects.filter(userprofile_id = 34).order_by('id')