Here is a snippet of how my models are setup:
class Profile(models.Model):
name = models.CharField(max_length=32)
accout = models.ManyToManyFie
I have this on a number of my models, but in my opinion (and unlike all the other answers) you shouldn't need to specify the order_by
again because, well, it's already specified in the through
model. Specifying it again breaks the DRY (don't repeat yourself) principle.
I would use:
qs = profile.profileaccounts_set.all()
This gives the set of ProfileAccounts associated with a profile using your configured ordering. Then:
for pa in qs:
print(pa.account.name)
For bonus points, you can also speed up the overall process by using select_related
in the query.