tags:

views:

26

answers:

1

I need to order_by a field comparison such that all fields matching a certain value are displayed at the top.

The SQL to do this is SELECT * FROM messages ORDER BY message='alsfkjsag' DESC

A: 

There are at least two ways to do it:

  1. Custom SQL with UNION:

    • combine two selects
    • one who contains all rows which have your desired message
    • the other with all rows who have another message
  2. Add a dynmaic Field to the QuerySet

    • extra(select={"is_message":"message='alsfkjsag'"})
    • and then order_by('is_message')
    • or in short:

    Messages.objects.extra(select={"is_message":"message='alsfkjsag'"}) .order_by('is_message')

Andre Bossard