Overview
When you have related objects that need to maintain a specific order (like slides in a slideshow, steps in a tutorial, or menu items), sortable inlines provide a user-friendly way to manage that order directly in the Django admin interface.
Basic Setup
To make an inline sortable, you need:- An ordering field in your model (typically an
IntegerFieldorPositiveIntegerField) - Set the
ordering_fieldattribute on your inline class
models.py
admin.py
The
ordering_field tells Unfold which field to update when items are reordered via drag-and-drop.Hiding the Ordering Field
You can hide the ordering field from the inline display while still maintaining sortable functionality:admin.py
Setting
hide_ordering_field = True provides a cleaner interface by hiding the numeric order field while keeping the drag-and-drop functionality.Stacked Inline Sorting
Sortable functionality works with both tabular and stacked inlines:admin.py
Model-Level Ordering
You can also enable ordering at the ModelAdmin level for the changelist view:admin.py
When
ordering_field is set on ModelAdmin, the field is automatically added to list_display and list_editable if not already present.Automatic Order Assignment
Implement automatic order assignment for new objects:models.py
admin.py
Combining with Tabs
Sortable inlines work seamlessly with inline tabs:admin.py
Learn more about inline tabs in Inline Tabs.
Combining with Pagination
Sortable inlines can be used with pagination, though sorting is limited to items on the current page:admin.py
Custom Ordering Logic
Implement custom ordering logic in your model:models.py
Best Practices
Use positive integers for ordering
Use positive integers for ordering
Always use Add a database index for better performance on large datasets.
PositiveIntegerField or PositiveSmallIntegerField for order fields:Set default ordering in Meta
Set default ordering in Meta
Always specify ordering in your model’s Meta class:This ensures consistent ordering throughout your application.
Handle gaps in ordering
Handle gaps in ordering
It’s okay to have gaps in order values. Focus on relative ordering, not sequential numbers:
Consider scope for ordering
Consider scope for ordering
Order items within a specific scope (parent object):
JavaScript Integration
The sortable functionality uses JavaScript to handle drag-and-drop. The order field is automatically updated when items are reordered:admin.py
Unfold handles all the JavaScript for drag-and-drop functionality automatically. You don’t need to include any additional scripts.
Technical Details
The name of the model field that stores the order value. This field must be numeric (IntegerField or similar).
When
True, hides the ordering field column from the inline display while maintaining drag-and-drop functionality.- Changelist:
unfold.templatetags.unfold_list.result_headersandresult_list - Inlines:
unfold.templatetags.unfold.inline_fieldsets
Related Features
Inlines Overview
Learn about inline basics
Inline Tabs
Organize inlines with tabs
Paginated Inlines
Add pagination to inlines
Nonrelated Inlines
Work with non-related objects