django-admin-sortable2 does not work

It stopped assigning order numbers and changing them whatsoever. Another reliable package betrayed me. Let’s figure out why.

At some point when I tried to drag and drop the bars, in Developer Mode -> Console I got Error 403, but I can’t replicate it anymore – it went away by itself.

How can I check the value if it is not visible aside from a HTML bar? In Django admin, you can inspect the ordering bar and see the order value:

<th class="field-_reorder">
    <a href="/admin/forumapp/section/5/change/">
        <div class="drag js-reorder-5 ui-sortable-handle" order="0">
            &nbsp;
        </div>
        </a>
</th>

It is clearly null, and that applies to everything else.

Attempt 1

Right off the bat I see a mistake I made. Instead of

my_order = models.PositiveIntegerField(default=0, blank=False, null=False)

I had:

my_order = models.PositiveIntegerField(default=0, blank=False, null=True)

Let’s try and change that and see how it goes. I made migrations and recreated all instances. To no avail.

Attempt 2

So far I tried using reorder and it worked, but never before I had to use it. Basically, you can do it manually:

python manage.py reorder yourapp.YourModel

Guidelines

In order to make inlines work, you need to reorder your through model (that has ordering):

python manage.py reorder yourapp.YourThroughModel

And then when you drag and drop the instances in SortableInlineAdminMixin, you need to save the instance where the inline is at. Say, my Section model has an inline with ordered Topics through model TopicInSection. When on the Section instance page, I reorder the TopicInSection instances, and in order to save this new order I need to Save the Section instance. I don’t know what happened before, but I did not need that (as I recall it).

Normal SortableAdminMixin work when you reorder the instance in a listing page of the model just fine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.