#15881: FilteredSelectMultiple does not respect order --------------------------------------+------------------------------------ Reporter: bmihelac | Owner: nobody Type: Cleanup/optimization | Status: new Component: contrib.admin | Version: 1.3 Severity: Normal | Resolution: Keywords: javascript | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 1 --------------------------------------+------------------------------------
Comment (by salmawisoky): The `FilteredSelectMultiple` widget in Django does not preserve the order of selected items by default. It displays the available choices in the order they are defined in the queryset, but the selected items are displayed in alphabetical order. If you want to maintain the order of selected items in the `FilteredSelectMultiple` widget, you can override the widget's rendering behavior by creating a custom widget that inherits from `FilteredSelectMultiple` and overrides the `render_option` method. [https ://drift-boss.pro Drift Boss] Here's an example of how you can create a custom widget that preserves the order of selected items: ```python from django.contrib.admin.widgets import FilteredSelectMultiple class OrderedFilteredSelectMultiple(FilteredSelectMultiple): def render_option(self, selected_choices, option_value, option_label): option_value = force_text(option_value) selected_html = ( ' selected="selected"' if option_value in selected_choices else '' ) return f'<option value="{option_value}"{selected_html}>{force_text(option_label)}</option>' ``` In this custom widget, the `render_option` method is overridden to remove the alphabetical sorting behavior. Instead, it checks if the option value is in the selected choices and adds the `selected` attribute accordingly. This ensures that the selected items are rendered in the order they appear in the selected choices. To use this custom widget in your form, specify it as the widget for your `MultipleChoiceField`: ```python from django import forms class MyForm(forms.Form): my_field = forms.MultipleChoiceField( widget=OrderedFilteredSelectMultiple(attrs={'size': 10}), choices=my_choices, ) ``` Replace `my_field` with the name of your field and `my_choices` with the choices for your field. By using the `OrderedFilteredSelectMultiple` widget instead of the standard `FilteredSelectMultiple`, the selected items in the `FilteredSelectMultiple` widget will be displayed in the order they were selected. -- Ticket URL: <https://code.djangoproject.com/ticket/15881#comment:4> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018a931839c0-163612c4-ff18-4516-aba6-f14afc488e5c-000000%40eu-central-1.amazonses.com.