20911 Fix sorting in dropdown #631

Closed
opened 2026-04-05 16:58:05 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @arthanson on 1/9/2026

Fixes: #20911

Two issues:

  1. The sorting order wasn't alphabetical
  2. Even with corrected sorting order, the selected item was show in the wrong ordering in the list (i.e. When you install a module into a Module Bay the select has a pre-set Module-bay, but when you do the dropdown - this pre-selected item shows up in the wrong order in the dropdown).

This fixes item 1, item 2 still appears but would need a front-end fix so keeping that for a separate ticket.

As noted in the issue discussion, the change to order_insertion_by will fix the ordering of newly inserted items, there were other ordering problems in bulk edit and bulk rename which this also fixes. I tried either looping and doing a save or doing a bulk_update and then a rebuild at the end, for small data-sets the bulk_update if much faster, for large data-sets they are almost the same perf but bulk_update still wins out.

Just changing order_insertion_by revealed an existing bug in the CSV import when using the order_insertion_by. We don't run into it in the tests for NestedGroupModel (the other model using it) because of the way the imports are ordered in the test case as they don't cause a re-arrangement in the tree. The ordering of the ModuleBay test actually does cause a re-ordering and hits what looks like a bug in MPTT code. The additional changes in bulk_views.py so the CSV processing if done on models that inherit form MPTTModel uses a delay_mptt_updates() and rebuild at the end fixes this issue.

*Originally created by @arthanson on 1/9/2026* ### Fixes: #20911 Two issues: 1. The sorting order wasn't alphabetical 2. Even with corrected sorting order, the selected item was show in the wrong ordering in the list (i.e. When you install a module into a Module Bay the select has a pre-set Module-bay, but when you do the dropdown - this pre-selected item shows up in the wrong order in the dropdown). This fixes item 1, item 2 still appears but would need a front-end fix so keeping that for a separate ticket. As noted in the issue discussion, the change to order_insertion_by will fix the ordering of newly inserted items, there were other ordering problems in bulk edit and bulk rename which this also fixes. I tried either looping and doing a save or doing a bulk_update and then a rebuild at the end, for small data-sets the bulk_update if much faster, for large data-sets they are almost the same perf but bulk_update still wins out. Just changing order_insertion_by revealed an existing bug in the CSV import when using the order_insertion_by. We don't run into it in the tests for NestedGroupModel (the other model using it) because of the way the imports are ordered in the test case as they don't cause a re-arrangement in the tree. The ordering of the ModuleBay test actually does cause a re-ordering and hits what looks like a bug in MPTT code. The additional changes in bulk_views.py so the CSV processing if done on models that inherit form MPTTModel uses a delay_mptt_updates() and rebuild at the end fixes this issue.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#631