title: Django model queryset usage of .extra() or union to apply a custom sort on a PositiveIntegerField
body: <p>For a gallery of items a user can sort manually some items to always make them appear first.</p>
  
  <p>For that the model have a "sort" not null PositiveIntegerField (default to 0). Items with sort field greater than zero are sorted manually and must appear before not sorted items with sort value at 0.</p>
  
  <p>The SQL query which may be used could be something like this:</p>
  
  <pre><code>(select photo, sort, 0 as priority from photos where sort &gt; 0)
  union all
  (select photo, sort, 1 as priority from photos where sort = 0)
  order by priority, sort
  </code></pre>
  
  <p>Results would looks like:</p>
  
  <pre><code>+---------+------+----------+
  | photo   | sort | priority |
  +---------+------+----------+
  | photo-a | 1    | 0        |
  | photo-b | 2    | 0        |
  | photo-c | 0    | 1        |
  | photo-d | 0    | 1        |
  | photo-e | 0    | 1        |
  | ...                       |
  </code></pre>
  
  <p>But I can't figure out how it should be done properly with Django Queryset? </p>
  
  <p>I'm able to merge two different queryset with <code>list(chain(q1, q2))</code> but queries are executed and this is inefficient with large collections, specially if my queryset should then be passed to django.core.paginator which normally would apply a LIMIT OFFSET clause for pagination. Of course, I could apply the slicing on each query <code>q1</code> and <code>q2</code> but this is a bit ugly as it overlap with what paginator does.</p>
  
  <p>Maybe I'm trying to do it the wrong way. Or perhaps it should be done with the <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#extra" rel="nofollow">extra()</a> method?</p>