Can "django-nested-admin" sort the top level items in select-to-change list in addition to inline items?

534 views Asked by At

I could sort inline items with django-nested-admin as shown below:

enter image description here

But, I couldn't sort the top level items in select-to-change list as shown below. (In addition, I also couldn't sort it with the combination of django-nested-admin and django-admin-sortable2):

enter image description here

This is the code in "models.py" as shown below:

# "models.py"

from django.db import models

class Country(models.Model):
    name = models.CharField(max_length=100)
    position = models.PositiveSmallIntegerField("Position", null=True, blank=True)

    class Meta:
        ordering = ('position',)

    def __str__(self):
        return self.name

class Province(models.Model):
    name = models.CharField(max_length=100)
    country = models.ForeignKey(Country, on_delete=models.PROTECT)
    position = models.PositiveSmallIntegerField("Position", null=True)

    class Meta:
        ordering = ('position',)

    def __str__(self):
        return self.name

class City(models.Model):
    name = models.CharField(max_length=100)
    province = models.ForeignKey(Province, on_delete=models.PROTECT)
    position = models.PositiveSmallIntegerField("Position", null=True)

    class Meta:
        ordering = ('position',)

    def __str__(self):
        return self.name

And, this is the code in "admin.py" as shown below:

# "admin.py"

from nested_admin import SortableHiddenMixin, NestedTabularInline, NestedModelAdmin
from .models import Country, Province, City

class CityInline(SortableHiddenMixin, NestedTabularInline):
    model = City
    sortable_field_name = "position"

class ProvinceInline(SortableHiddenMixin, NestedTabularInline):
    model = Province
    sortable_field_name = "position"
    inlines = (CityInline,)

@admin.register(Country)
class CountryInlineAdmin(SortableHiddenMixin, NestedModelAdmin):
    sortable_field_name = "position"
    inlines = (ProvinceInline,)

Are there any ways to sort the top level items in select-to-change list?

Or, is it impossible to sort the top level items with django-nested-admin?

1

There are 1 answers

0
Super Kai - Kazuya Ito On BEST ANSWER

django-nested-admin hasn't supported the sort for the top level items in select-to-change list yet. Actually, the feature was already requested as you can see Add sorting support for changelist view.

In addition, the combination of django-nested-admin and django-admin-sortable2 doesn't work because they're not integrated each other.