Retrieving elements in a specific order in django
If you have a list of ordered ids and you want to turn them into an ordered result set you can use FIELD() in mysql:
SELECT * FROM addons
ORDER BY FIELD(id, 3, 5, 9, 1);
This is a handy trick if you use an external search engine which gives you an ordered list of ids and you want to pull out entire row sets.
ids = [a_list, of, ordered, ids]
addons = Addon.objects.filter(id__in=ids).extra(
select={'manual': 'FIELD(id,%s)' % ','.join(map(str,ids))},
order_by=['manual'])
Читайте також:
- Ordering related objects in Django
- Python + CouchDB
- Django admin filter by ranges of dates with calendar
- Fix Django 1.4 admin_site ForeignKeyRawIdWidget issue
- How to expire session on browser close in django
- Django admin filter by ranges of ages
- Django yandex and google maps integration
- MemcachedKeyLengthError: Key length is > 250
- django-stdimage traceback "The '%s' attribute has no file associated with it"
- PHP бібліотеки для Mac OS X Leopard 64 bit gd.so, pdo_mysql.so, mcrypt.so та інші