layers Introducing Unfold components - Pre-made HTML blocks in Django admin for custom dashboards arrow_forward

Sortable inlines

Unfold allows you to sort inlines by adding a ordering_field to the inline class. This field will be used to sort the inlines in the admin panel. There is also an option to hide the ordering field from the UI by setting hide_ordering_field to True.

  • The model field used for ordering must be a PositiveIntegerField with db_index=True
  • Sorting newly added records does not work. You need so save new records first and then sort
  • The sorting functionality is not available on changelist view.
# 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

In order to use the sorting functionality, you need to create a model field with a PositiveIntegerField type and set db_index=True where the Unfold admin will store the sorting order.

# 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)

Be first to know about new features and updates

Each time something new happens in Unfold, like new major features or new design components, we'll send you a newsletter. We promise not to spam you.

Django admin theme built with Tailwind CSS to bring modern look and feel to your admin interface. Already contains several built-in features for smooth developer experience.

© 2023 - 2024 Created by unfoldadmin.com. All rights reserved.