Django ManyToManyField ordering using through

后端 未结 7 2353
傲寒
傲寒 2020-12-13 20:27

Here is a snippet of how my models are setup:

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

    accout = models.ManyToManyFie         


        
7条回答
  •  鱼传尺愫
    2020-12-13 20:43

    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',)
    

提交回复
热议问题