title: Django: Selecting queryset fields based on a condition
body: <p>Folks,</p>
  
  <p>Here's the schema of the table T (Let's say Model name is also T with fields A, B and C):</p>
  
  <pre><code> A    B   C
  ---  --- ---
   1    a   b
   2    b   NULL
   3    c   c
  </code></pre>
  
  <p>Now, I want to select values (A, C or B). I can select A and C as follows:</p>
  
  <pre><code> T.objects.all().values_list('A','C')
  </code></pre>
  
  <p>But it will give me A and C values only. All I want is if C is NULL, then I should get B. I don't know whether I can achieve this directly using any condition or join (Sorry, I'm not a SQL guy) but I can definitely achieve this using follows:</p>
  
  <pre><code> [(x,z or y) for (x,y,z) in T.objects.all().values_list('A','B','C')]
  </code></pre>
  
  <p>So, there are two open questions:</p>
  
  <ol>
  <li><p>Is there any <strong>better way</strong> of doing it? (Preferably using Queryset/Django ORM functions)</p></li>
  <li><p>In case of tens of thousands of records, what is the <strong>memory efficient / optimized way</strong> of doing this? Is there any way other than limiting the queryset into smaller chunks (let's say 1000 records at once) using the loop (assuming ordered primary key) as follows:</p>
  
  <blockquote>
  <pre><code>max_pkey = T.objects.all().order_by('-pk')[0]
  current = 0
  chunk = 1000
  while current &lt;= max_pkey:
      objs = T.objects.filter(pk__gt=current)[:chunk]
      Process the objects
  </code></pre>
  </blockquote></li>
  </ol>