Django Unfold provides a powerful sorting capability for inlines through the ordering_field
attribute in the inline class. When specified, this field determines the sorting order of inlines within the admin panel. For enhanced UI customization, you can optionally hide the ordering field from the interface by setting hide_ordering_field
to True
.
Important considerations when implementing sortable inlines:
PositiveIntegerField
with db_index=True
for optimal performance# admin.py
from unfold.admin import TabularInline
from .models import User
# This works for StackedInline as well
class MyInline(TabularInline):
model = User
ordering_field = "weight"
hide_ordering_field = True
list_display = ["email", "weight"] # Weight is mandatory field
To enable sorting functionality, create a model field of type PositiveIntegerField
with db_index=True
. This field will be used by the Unfold admin to maintain the sorting order of your records.
# models.py
from django.db import models
from django.utils.translation import gettext_lazy as _
class User(models.Model):
weight = models.PositiveIntegerField(_("weight"), default=0, db_index=True)
© 2023 - 2025 Created by unfoldadmin.com. All rights reserved.