多对多有序记录django
many to many ordered records django
我正在使用 user.companies.all() 获取所有相关记录。
但是这个查询给了我来自 userProfile_companies table 的随机记录。我需要在桥接 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)
我得到的公司 ID 顺序不正确。
谢谢
通过 table 访问是这里的解决方案。 here's the post.
UserProfile.companies.through.objects.filter(userprofile_id = 34).order_by('id')
我正在使用 user.companies.all() 获取所有相关记录。
但是这个查询给了我来自 userProfile_companies table 的随机记录。我需要在桥接 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)
我得到的公司 ID 顺序不正确。 谢谢
通过 table 访问是这里的解决方案。 here's the post.
UserProfile.companies.through.objects.filter(userprofile_id = 34).order_by('id')