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()
r1.name = ‘Tom’
r1.save()
r2 = Test()
r2.name = ‘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()
r1.name = ‘Sam’
r2 = Test.objects.filter(id=2).first()
r2.name = ‘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?