Django Admin Sortable 2 Not Saving Order

Question:

I have installed Django Admin Sortable 2 in to my Django project. I can see the drag and drop order system in the admin but when I move around some objects the order doesn’t get saved. I cannot see any error in my terminal log either when moving objects around.

Here is my Model and Admin python files. Do I need to do anything extra to get the order to save?

Model.py

class QuickLink(models.Model):
    title = models.CharField(max_length=20)
    image = models.FileField(null=True, blank=False,upload_to='media/quick_links')
    link = models.CharField(max_length=200)
    order = models.PositiveIntegerField(default=0, blank=False, null=False)

    def __str__(self):
        return self.title

    class Meta(object):
        ordering = ['order']

Admin.py

from django.contrib import admin
from adminsortable2.admin import SortableAdminMixin
from .models import QuickLink

@admin.register(QuickLink)
class QuickLinksAdmin(SortableAdminMixin, admin.ModelAdmin):
    pass

My versions are as follows:

Django 2.0.4

Python 3.6.1

Django Admin Sortable 2 0.6.19

Asked By: samroberts707

||

Answers:

Try running management command:
./manage.py reorder <app.model>

Answered By: Ramil Aglyautdinov

The reason I was having problems re-ordering the objects was I had already created the objects when then adding the Sortable functionality.

When setting a default for this as 0 it would set them all for 0. By going back and either recreating the objects or editing the field in the DB to the correct order I was able to fix this.

Answered By: samroberts707

Also it’s possible to create data migration which set initial order for already created model.

def reorder(apps, schema_editor):
    Tag = apps.get_model("main", "Tag")
    order = 0
    for item in Tag.objects.all():
        order += 1
        item.order = order
        item.save()


class Migration(migrations.Migration):

    dependencies = [
        ('main', '0011_auto_20190517_1336'),
    ]

    operations = [
        migrations.RunPython(reorder, RunPython.noop),
Answered By: Arseniy Lebedev

For the record, in my case, it was not recording because of several reasons which each prevented the index saving:

1/ In the admin inline, I displayed the index in the readonly_fields.

readonly_fields=[ ...,'myindex',...]

I solved the issue by defining a fonction to display the index:

readonly_fields=[ ...,'myindex',...]
def index(self,obj): #pour pouvoir afficher l'index 
    return obj.myindex

2/ I had in my model:

class Meta:
    order_with_respect_to = 'series'

and in my admin inline:

ordering=['myindex']

in order to have in the admin_change page the objects ordered by parent field to have all related fields displayed together
and in the inline the records ordered by index

…but that prevented the index to be saved when records were moved in the inline

I therefore removed both and put in the model:

class Meta:
    ordering=['myindex']

With both changes performed, it works.
I tested and if only one of the changes is done, it still does not record the change when saving

Answered By: Skratt

Run this command to save your sorting:

python manage.py reorder <app.model>

After running this command above, your sorting will be saved.

In your case, I don’t know your app name so if your app name is "store" and I know your model name "QuickLink" so running this command below will solve your problem:

python manage.py reorder store.QuickLink

Buy me a coffee!!

Answered By: Kai – Kazuya Ito