Django update() to swap values of two fields in MySQL?
<p>The following does not work:</p>

<pre><code>Car.objects.filters(&lt;filter&gt;).update(x=F('y'), y=F('x'))
</code></pre>

<p>as both <code>x</code> and <code>y</code> ends up being the same value. </p>

<p>I need to use update() instead of save() due to performance (large set of records).</p>

<p>Are there any other way of doing an update like the one above to mimic Python's <code>x, y = y, x</code>?</p>

<p>The db is MySQL, <a href="http://dba.stackexchange.com/questions/31045/swap-columns-on-some-rows/31052#31052">which might explain why the resulting SQL statement doesn't work</a>.</p>

<p>I don't think update can do that as it's basically an sql wrapper.
What you could do, though, is use save(values=['x','y']). Hopefully it won't be as slow.
Alternatively, you could use raw sql from django to perform the swap (see <a href="http://stackoverflow.com/questions/2758415/swap-values-for-two-rows-in-the-same-table-in-sql-server">Swap values for two rows in the same table in SQL Server</a>)</p>

python mysql django