Django ManyToMany intermediary model, move relationship

431 Views Asked by At

So, after reading the documentation regarding intermediary model's, it seems that there's no built-in way of changing one side of a relationship without clearing all relationships with that side.

But how can I do this anyway? I was thinking that I may have to store all of the relationships in an array / object so I can recreate them when needing to change 1 or more relations.

My Models:

Company
Camera
CompanyCameraRelationship

I would want to change the relationship between Company 1 and Camera 1 to Company 1 and Camera 2. So in order to do this, I must clear all relationships between Company 1 and other Cameras?

Any thoughts would be greatly appreciated!

1

There are 1 best solutions below

3
On BEST ANSWER

Do you mean you want to change the existing intermediary objects? You can update them using the intermediary model.

CompanyCameraRelationship.objects.filter(company='Company 1', camera='Camera 1').update(camera='Camera 2')