title: How can I make a Django update with a conditional case?
body: <p>I would like to use Django to update a field to a different value depending on its current value, but I haven't figured out how to do it without doing 2 separate update statements.</p>
  
  <p>Here's an example of what I'd like to do:</p>
  
  <pre><code>now = timezone.now()
  data = MyData.objects.get(pk=dataID)
  if data.targetTime &lt; now:
      data.targetTime = now + timedelta(days=XX)
  else:
      data.targetTime = data.targetTime + timedelta(days=XX)
  data.save()
  </code></pre>
  
  <p>Now, I'd like to use an update() statement to avoid overwriting other fields on my data, but I don't know how to do it in a single update(). I tried some code like this, but the second update didn't use the up to date time (I ended up with a field equal to the current time) :</p>
  
  <pre><code># Update the time to the current time
  now = timezone.now()
  MyData.objects.filter(pk=dataID).filter(targetTime__lt=now).update(targetTime=now)
  # Then add the additional time
  MyData.objects.filter(pk=dataID).update(targetTime=F('targetTime') + timedelta(days=XX))
  </code></pre>
  
  <p>Is there a way I can reduce this to a single update() statement? Something similar to the SQL CASE statement?</p>