Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ManyToManyField ordering using through

Here is a snippet of how my models are setup:

class Profile(models.Model):     
    name = models.CharField(max_length=32)

    accout = models.ManyToManyField(
        'project.Account',
        through='project.ProfileAccount'
    )

    def __unicode__(self)
        return self.name

class Accounts(models.Model):
    name = models.CharField(max_length=32)
    type = models.CharField(max_length=32)

    class Meta:
        ordering = ('name',)

    def __unicode__(self)
        return self.name

class ProfileAccounts(models.Model):
    profile = models.ForeignKey('project.Profile')
    account = models.ForeignKey('project.Accounts')

    number = models.PositiveIntegerField()

    class Meta:
        ordering = ('number',)

Then when I access the Accounts, how can I sort by 'number' in the intermediary ProfileAccounts model, rather than the default 'name' in the Accounts Model?:

for acct_number in self.profile.accounts.all():
    pass

This does not work, but is the gist of how I want it to access this data:

for scanline_field in self.scanline_profile.fields.all().order_by('number'):
    pass
like image 777
Brian Avatar asked Oct 08 '10 20:10

Brian


3 Answers

I just came through this.

class Profile(models.Model):          accounts = models.ManyToManyField('project.Account',                                       through='project.ProfileAccount')      def get_accounts(self):         return self.accounts.order_by('link_to_profile')   class Account(models.Model):     name = models.CharField(max_length=32)   class ProfileAccount(models.Model):     profile = models.ForeignKey('project.Profile')     account = models.ForeignKey('project.Account', related_name='link_to_profile')     number = models.PositiveIntegerField()      class Meta:         ordering = ('number',) 

I removed the fields which were off-topic except of Account.name. That's the shortest solution I've found, no idea if it was possible to use in 2010, but it certainly is now.

like image 100
zalun Avatar answered Oct 13 '22 19:10

zalun


a ManyToManyField manager allows you to select/filter data from the related model directly, without using any model connection of the through model on your django level...

Likewise,

if you try:

pr = Profile.objects.get(pk=1)
pr.account.all()

returns you all account related to that profile. As you see, there exists no direct relation to the through model ProfileAccount, so you can not use the M2M relation at this point... You must use a reverse relation to the through model and filter the results...

pr = Profile.objects.get(pk=1)
pr.profileaccount_set.order_by('number')

will give you an ordered queryset, but, in this case, what you have in queryset is profileaccount objects, not account objects... So you have to use another django level relation to go to each related account with:

pr = Profile.objects.get(pk=1)
for pacc in pr.profileaccount_set.order_by('number'):
    pacc.account
like image 20
FallenAngel Avatar answered Oct 13 '22 21:10

FallenAngel


Add the related name to ProfileAccounts and then change the ordering in Accounts with that 'related_name__number'. Note two underscores between related_name and number. See below:

class Accounts(models.Model):
    .
    .
    .
    class Meta:
        ordering = ('profile_accounts__number',)


class ProfileAccounts(models.Model):
    .
    .
    .
    account = models.ForeignKey('project.Accounts', related_name='profile_accounts')

    number = models.PositiveIntegerField()

    class Meta:
        ordering = ('number',)
like image 20
shabss Avatar answered Oct 13 '22 19:10

shabss