Hello peeps, I have an issue which is driving me against the wall. I have a an ORM setup where I create a VirtualMachine
model and then create a ManyToMany relationship with django users
owners = models.ManyToManyField(User,blank=True, db_index=True)
Later I have a method where I might need to change the owners of the VM. To do this, I use set()
like so
existing_owners = list(existing_vm.owners.order_by("username").all())
for owner_uuid in vm_json.get('members',[]):
owner = find_or_create_user(owner_uuid)
if owner is not None:
owners.append(owner)
if existing_owners != owners:
logger.info(f"VM {existing_vm.name} owners list differ from the DB. Updating DB with new owners.")
existing_vm.owners.set(owners, clear=True)
This all runs without an errors but…doesn’t change the relationship. Adding logging before and after the operation, I can clearly see that the owners
var is different, but after the set()
the DB still reports the old relationships. I made sure that this is not any caching issue by restarting the app and subsequent operations confirm the owners never changed.
Given that there’s no errors whatsoever, I’m struggling to understand how I can troubleshoot this.
Any advise is appreciated.