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']
You would want to do an ordering of 'logo__title' to reach to the attribute title of the logo object (wagtailimages.Image).
Remember, when altering the meta class you will have to make migrations and migrate for your ordering to occur.
class Meta:
ordering = ['logo__title']