Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Use in in django while preserving order

pk_list = [5, 7, 1, 3, 4]
clauses = ' '.join(['WHEN id=%s THEN %s' % (pk, i) for i, pk in enumerate(pk_list)])
ordering = 'CASE %s END' % clauses
queryset = Article.objects.filter(pk__in=pk_list).extra(
           select={'ordering': ordering}, order_by=('ordering',))
Source by codybonney.com #
 
PREVIOUS NEXT
Tagged: #Use #django #preserving #order
ADD COMMENT
Topic
Name
7+5 =