How to exchange the key field in a transaction?

class Test(models.Model):
id = models.AutoField(primary_key=True)
name = models.TextField(null=False,blank=False, unique=True)
class Meta:
db_table = ‘test’

Add two records to this table:
r1 = Test() = ‘Tom’

r2 = Test() = ‘Sam’
r2. save()

Now I want to exchange the names in these two record in a transaction, how?

r1 = Test.objects.filter(id=1).first() = ‘Sam’

r2 = Test.objects.filter(id=2).first() = ‘Tom’

with transaction.atomic():
Test.objects.bulk_update([r1, r2], fields=[‘name’])

Previous code will raise an IntegrityError: ‘Sam’ is exists.
How can I save these two record in a transaction?

Use the old “three-step” exchange technique

  • Change to some random unique value
  • Change to the original value
  • Change to the original value

It’s three db updates instead of two, but it does keep the constraints satisfied.

Hi, KenWhitesell:
Thank you for all your assistance. I use this method now.