3

I don't want any ordering to be applied to a query. So, I have a QuerySet follow as:

question_obj = Question.objects.filter(pk__in=[100,50,27,35,10,42,68]).order_by()

However, when I retrieve the results, they are always ordered by questionID. I iterate the question_obj and this is the result:

for obj in question_obj:
   obj.questionID

The result is displayed such as:

10L
27L
35L
42L
50L
68L
100L
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Thinh Phan
  • 655
  • 1
  • 14
  • 27

4 Answers4

2

If you want an unordered collection, use Python's Set object, documented here: http://docs.python.org/tutorial/datastructures.html#sets

If you want the ordering to be the same as the list you're passing as the value for pk__in, you could try:

ids = [100,50,27,35,10,42,68]
questions = list(Question.objects.filter(pk__in=ids))
question_obj = sorted(questions, key=lambda x: ids.index(x.id))

EDIT: And because it's extremely unclear as to what you mean by 'unordered' in reference to a data structure that is by definition ordered: Random ordering can be accomplished through the following:

.order_by('?')
fourk
  • 2,224
  • 1
  • 16
  • 10
2

If you want to display the objects in the same order as the list of primary keys, then you could use in_bulk to create a dictionary keyed by pk. You can then use a list comprehension to generate the list of questions.

>>> pks = [100,50,27,35,10,42,68]
>>> questions_dict = Question.objects.in_bulk(pks)
>>> questions = [questions_dict[pk] for pk in pks]
>>> for question in questions:
        print question.pk
100
50
27
35
...
Alasdair
  • 298,606
  • 55
  • 578
  • 516
0

Luke, use the Source, er, the Docs! Yeah, that's it!

Django QuerySet API - order_by()

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
  • I think he wants to NOT order them by a particular field. Instead order them in the order they are suggested in the provided list – Timmy O'Mahony Nov 09 '11 at 01:56
  • Dear Peter Rowell, I used the "Django QuerySet API - order_by()" in my statement but it is not effective. question_obj = Question.objects.filter(pk__in=[100,50,27,35,10,42,68]).order_by() It always return the order of questionID. – Thinh Phan Nov 09 '11 at 03:15
  • 1
    @ThinhPhan: I read "I don't want any ordering to be applied to a query" to mean literally that you wanted a random order. Given the answer you accepted as correct what you *meant* to say was "I want the objects returned in the order given by the IDs." Interestingly enough the `order_by('?')` operator silently does *nothing* when the query is given an explicit list of IDs (behavior which I consider to be a bug). – Peter Rowell Nov 09 '11 at 19:25
0

You could do some raw SQL (with FIELD()) a lá:

Ordering by the order of values in a SQL IN() clause

which should allow you to retrieve them in the order suggested in the list.

To run custom SQL with the ORM:

https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly

Community
  • 1
  • 1
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177