1👍
✅
It’s because the channel
field on your CustomUser
model has primary_key=True
.
This means when you do:
In [96]: u.channel = c2
In [97]: u.save()
…you have changed the primary key of u
, therefore when you save it is creating a new user, but with the same username as the original one, hence the IntegrityError.
I think you need to do it like this instead:
u = CustomUser.objects.get(username='django')
u.delete()
u.channel = c2
u.save()
Source:stackexchange.com