如果您使用的是符合标准的SQL数据库,这应该可以正常工作。查询将扩展到UPDATE car SET x = y, y = x WHERE
它至少可以在PostgreSQL(也在下面)、SQLite3(如下)、Oracle和{a3}上正常工作,但是MySQL的实现被破坏了。在
PostgreSQL:
^{pr2}$
SQLite3sqlite> select * from foo;
prefect|ford
sqlite> update foo set x = y, y = x;
sqlite> select * from foo;
ford|prefect
但是,SQL违反了标准mysql> insert into car values ('prefect', 'ford');
Query OK, 1 row affected (0.01 sec)
mysql> select * from car;
+ -+ +
| x | y |
+ -+ +
| prefect | ford |
+ -+ +
1 row in set (0.00 sec)
mysql> update car set x = y, y = x;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from car;
+ + +
| x | y |
+ + +
| ford | ford |
+ + +
1 row in set (0.00 sec)
因此,有一种使用标准兼容的SQL数据库来实现这一点的可移植方法,但MySQL不是其中之一。如果您不能使用for。。。save循环,那么您必须求助于Swapping column vales in MySQL中的一些技巧;临时变量似乎是最通用的;尽管我不确定是否可以将临时变量与Django的F结构一起使用。在