0

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!

dcolumbus
  • 9,596
  • 26
  • 100
  • 165

1 Answers1

0

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')
A. J. Parr
  • 7,731
  • 2
  • 31
  • 46
  • WHAT!? You're kidding me ... why isn't this at all mentioned in the documentation? And if it is, can you point it out to me? – dcolumbus May 18 '17 at 04:15
  • @dcolumbus You can work with the through model like any other model. https://docs.djangoproject.com/en/1.11/topics/db/models/#extra-fields-on-many-to-many-relationships Describes how the through models can be used. – A. J. Parr May 18 '17 at 07:57
  • Brilliant! Thank you very much. – dcolumbus May 18 '17 at 16:05