22👍
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))
10👍
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]
- [Django]-Using {% url ??? %} in django templates
- [Django]-Django plural for templates
- [Django]-What is "load url from future" in Django
4👍
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;
- [Django]-How can I get the object count for a model in Django's templates?
- [Django]-Python Django Templates and testing if a variable is null or empty string
- [Django]-How to execute a Python script from the Django shell?
1👍
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'])
- [Django]-What does on_delete=models.PROTECT and on_delete=models.CASCADE do on Django models?
- [Django]-Django – present current date and time in template
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
1👍
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
- [Django]-How to remove all of the data in a table using Django
- [Django]-Django datefield filter by weekday/weekend
- [Django]-Django, creating a custom 500/404 error page
0👍
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',))
- [Django]-Success_url in UpdateView, based on passed value
- [Django]-Django bulk_create function example
- [Django]-Which Model Field to use in Django to store longitude and latitude values?