Ordering custom fields in django admin

I have a User model with the following fields:

#models.py
class User(models.Model):
  ...
  first_name = models.CharField(max_length=255)
  middle_name = models.CharField(max_length=255, null=True, blank=True, default="")
  last_name = models.CharField(max_length=255)
  ...

I have written a custom field like bellow that can be ordered with first_name, middle_name and last_name:


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    list_display = ("username", "email", "full_name", "is_staff", "date_joined")
    list_filter = ("is_staff", "is_active")

    @admin.display(ordering=("first_name", "middle_name", "last_name"))
    def full_name(self, user: User):
        return f"{user.first_name} {user.middle_name} {user.last_name}"

But admin.display(ordering=...) doesn't work with multiple fields like above.

How to make this custom field ordered by multiple fields?

Back to Top