开发者

Django: __in query lookup doesn't maintain the order in queryset

I have ID's in a specific order

>>> album_ids = [2开发者_如何转开发4, 15, 25, 19, 11, 26, 27, 28]
>>> albums = Album.objects.filter( id__in=album_ids, published= True )
>>> [album.id for album in albums]
[25, 24, 27, 28, 26, 11, 15, 19]

I need albums in queryset in the same order as id's in album_ids. Anyone please tell me how can i maintain the order? or obtain the albums as in album_ids?


Assuming the list of IDs isn't too large, you could convert the QS to a list and sort it in Python:

album_list = list(albums)
album_list.sort(key=lambda album: album_ids.index(album.id))


You can't do it in django via ORM. But it's quite simple to implement by youself:

album_ids = [24, 15, 25, 19, 11, 26, 27, 28]
albums = Album.objects.filter(published=True).in_bulk(album_ids) # this gives us a dict by ID
sorted_albums = [albums[id] for id in albums_ids if id in albums]


For Django versions >= 1.8, use below code:

from django.db.models import Case, When

field_list = [8, 3, 6, 4]
preserved = Case(*[When(field=field, then=position) for position, field in enumerate(field_list)])
queryset = MyModel.objects.filter(field__in=field_list).order_by(preserved)

Here is the PostgreSQL query representation at database level:

SELECT *
FROM MyModel
ORDER BY
  CASE
    WHEN id=8 THEN 0
    WHEN id=3 THEN 1
    WHEN id=6 THEN 2
    WHEN id=4 THEN 3
  END;


You can do it in Django via ORM using the extra QuerySet modifier

>>> album_ids = [24, 15, 25, 19, 11, 26, 27, 28]
>>> albums = Album.objects.filter( id__in=album_ids, published= True
             ).extra(select={'manual': 'FIELD(id,%s)' % ','.join(map(str, album_ids))},
                     order_by=['manual'])


Using @Soitje 's solution: https://stackoverflow.com/a/37648265/1031191

def filter__in_preserve(queryset: QuerySet, field: str, values: list) -> QuerySet:
    """
    .filter(field__in=values), preserves order.
    """
    # (There are not going to be missing cases, so default=len(values) is unnecessary)
    preserved = Case(*[When(**{field: val}, then=pos) for pos, val in enumerate(values)])
    return queryset.filter(**{f'{field}__in': values}).order_by(preserved)


album_ids = [24, 15, 25, 19, 11, 26, 27, 28]
albums =filter__in_preserve(album.objects, 'id', album_ids).all()

Note that you need to make sure that album_ids are unique.

Remarks:

1.) This solution should safely work with any other fields, without risking an sql injection attack.

2.) Case (Django doc) generates an sql query like https://stackoverflow.com/a/33753187/1031191

order by case id 
          when 24 then 0
          when 15 then 1
          ...
          else 8 
end


If you use MySQL and want to preserve the order by using a string column.

words = ['I', 'am', 'a', 'human']
ordering = 'FIELD(`word`, %s)' % ','.join(str('%s') for word in words)
queryset = ModelObejectWord.objects.filter(word__in=tuple(words)).extra(
                            select={'ordering': ordering}, select_params=words, order_by=('ordering',))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜