Django MultipleChoiceField does not preserve order of selected values

谁都会走 提交于 2019-12-10 15:41:47

问题


I have a Django ModelForm which exposes a multiple choice field corresponding to a many-to-many relation through a model which holds order of selection (a list of documents) as an extra attribute. At the front-end, the field is displayed as two multiple select fields similar to that in admin, one to list available choices and the other holds the selected elements.

The form can be saved with the correct selection of elements but they are always in the order of the original order of choices, not the selection. The browser sends the selection in correct order, but order in form.cleaned_data['documents'] is always the order in original order of choices.

How can I make the MultipleChoiceField respect the order of elements selected?

Thanks.


回答1:


There is no simple way. You either need to override the clean method of the MultipleChoiceField or, as you mentioned in your comment, use the getlist to re-order them manually. It probably depends how often in your code do you need to do it.

The clean method of MultipleChoiceField creates a QuerySet that you are receiving, by filtering an object list through the IN operator like this, so the order is given by the database:

qs = self.queryset.filter(**{'%s__in' % key: value})

You can inherit from ModelMultipleChoiceField:

class OrderedModelMultipleChoiceField(ModelMultipleChoiceField):
    def clean(self, value):
        qs = super(OrderedModelMultipleChoiceField, self).clean(value)
        return sorted(qs, lambda a,b: sorted(qs, key=lambda x:value.index(x.pk)))

The drawback is that the returned value is no longer a QuerySet but an ordinary list.




回答2:


To return an ordered QuerySet when overriding the clean method you could also do this:

class OrderedModelMultipleChoiceField(ModelMultipleChoiceField):
    def clean(self, value):
        qs = super(OrderedModelMultipleChoiceField, self).clean(value)
        clauses = ' '.join(['WHEN id=%s THEN %s' % (pk, i) for i, pk in enumerate(value)])
        return qs.filter(pk__in=value).extra(
            select={'ordering': 'CASE %s END' % clauses},
            order_by=('ordering',)
        )



回答3:


I did it via a Widget. The benefit of it is, it will sort properly in different languages:

class SortedSelectMultiple(SelectMultiple):

def render_options(self, selected_choices):
    self.choices = sorted(self.choices)
    self.choices.sort(key=lambda x: x[1])
    return super(SortedSelectMultiple, self).render_options(selected_choices)


来源:https://stackoverflow.com/questions/10296333/django-multiplechoicefield-does-not-preserve-order-of-selected-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!