0

I have model that I need to be able to order in the admin panel alphabetically by the title of the image foreign key. Currently with the code below the models get ordered by when the associated 'logo' images were added to the database, as opposed to by the titles of the associated 'logo' images.

class Client(models.Model):
    logo = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )
    homepage_visible = models.BooleanField(default=True)

    panels = [
        MultiFieldPanel([
            ImageChooserPanel('logo'),
            FieldPanel('homepage_visible'),
        ], heading='Client information'),
    ]

    def __str__(self):
        return self.logo.title

    class Meta:
        verbose_name = 'Client Logo'
        verbose_name_plural = 'Client Logos'
        ordering = ['logo']
4

1 回答 1

1

您可能希望对 'logo__title' 进行排序以到达徽标对象 (wagtailimages.Image) 的属性标题。

请记住,在更改元类时,您必须进行迁移并迁移以使您的排序发生。

class Meta:
    ordering = ['logo__title']
于 2017-11-14T01:17:20.703 回答