Changes between Initial Version and Version 1 of Ticket #32685
- Timestamp:
- Apr 26, 2021, 5:31:09 AM (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #32685 – Description
initial v1 5 5 So, I'd do something like 6 6 {{{ 7 queryset.filter(my_field__in= values).raw(8 f'order by array_position(ARRAY[{ ",".join(["%s"]*len( values)) }]::varchar[], my_field)',9 params= values)7 queryset.filter(my_field__in=my_values).raw( 8 f'order by array_position(ARRAY[{ ",".join(["%s"]*len(my_values)) }]::varchar[], my_field)', 9 params=my_values) 10 10 }}} 11 11 but this does not work... 12 12 is there a solution here? (I cannot use raw either since I have a queryset input argument to work with) 13 13 14 **UPDATE:** 15 16 now, I've found a cryptic solution: 17 https://stackoverflow.com/a/37648265/1031191 18 19 {{{ 20 from django.db.models import Case, When 21 22 preserved = Case(*[When(my_field=val, then=pos) for pos, val in enumerate(my_values)]) 23 queryset.filter(my_field__in=my_values).order_by(preserved) 24 }}} 25 I think Django should provide a better way than this. 26 27 Maybe {{{ queryset.filter(my_field__in_preserve=my_values) }}}