1

I have a model:

class MyModel(models.Model):
  name = models.TextField()
  age  = models.IntegerField(max_length=3)

I have names like: abraham lincoln, john kennedy, someone else, doniyor and many other

I want to sort them by name with this logic:

  • show all people with name john kennedy first.

the other people should be shown after them.

I am a bit stuck because order_by() takes fieldname and not values as parameter. How do I do this in django?

doniyor
  • 36,596
  • 57
  • 175
  • 260
  • what is the criteria for you to put `John Kennedy` first ? – karthikr Nov 25 '13 at 18:16
  • @karthikr, john kennedy is just example. i have a list of 100 of people and i want to make them sortable. But keep the other items at the bottom. this is my goal – doniyor Nov 25 '13 at 18:23
  • I got that, What i meant was, what is the criteria for putting items on top ? Example, `age` > 10, etc.. – karthikr Nov 25 '13 at 18:42

2 Answers2

3

You can do this by joining two querysets, first that contains all the names with john kennedy and second one that contains all the other names.

from django.db.models import Q
queryset1 = MyModel.objects.filter(name='john kennedy')
queryset2 = MyModel.objects.filter(~Q(name='john kennedy'))
queryset =  queryset1 | queryset2
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    @doniyor Yes you can, but put the keyword argument `age=30` after the [Q object](https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects): `filter(~Q(name='john kennedy'), age=30)` – Ashwini Chaudhary Nov 25 '13 at 18:40
  • @karthikr Feel free to edit my answer. I don't have access to Django right now, so can't test it. ;-) But as docs suggest all keyword arguments must come after the `Q` objects. – Ashwini Chaudhary Nov 25 '13 at 18:45
  • @hcwhsa Sorry, you are right. I dont know what i was thinking :P – karthikr Nov 25 '13 at 18:47
2

Use two QuerySet objects:

from itertools import chain

q1 = MyModel.objects.filter(name='john kennedy')
q2 = MyModel.objects.exclude(name='john kennedy')
results = list(chain(q1, q2))

You're not really ordering MyModel objects by any criteria; you're only taking some of them and putting them first. Hence this can be expressed using two queries.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180