Hi,
I have a many-to-many relationship between users and things, where a user can select multiple things,
and a thing can be selected by multiple users. The relationship between users and things also
contains data, so I've specifically setup a through table.
When I add a user to a thing, I guess I expect a one-to-one relationship between the thing and
the intervening bridge, but it seems like I'm getting a one-to-many.
Here is my code:
models.py:
class TestUser(models.Model):
user_data = models.TextField(default="")
def __str__(self):
return "Other: " + self.user_data
class TestThing(models.Model):
thing_data = models.TextField(default="")
user = models.ManyToManyField(TestUser, through='TestBridge')
def __str__(self):
return "Thing: " + self.thing_data
class TestBridge(models.Model):
user = models.ForeignKey(TestUser, on_delete=models.CASCADE)
thing = models.ForeignKey(TestThing, on_delete=models.CASCADE)
bridge_data = models.TextField(default="")
def __str__(self):
return "Bridge: " + self.bridge_data
tests.py:
u_1 = TestUser(user_data = 'user')
u_1.save()
t_1 = TestThing(thing_data='thing 1')
t_1.save()
t_2 = TestThing(thing_data='thing 2')
t_2.save()
t_1.user.add(u_1, through_defaults={'bridge_data': 'bridge 1'})
t_2.user.add(u_1, through_defaults={'bridge_data': 'bridge 2'})
data = list(TestThing.objects.all().values('user__user_data',
'thing_data',
'user__testbridge__bridge_data'))
for item in data:
print(item)
Output:
{'user__user_data': 'user', 'thing_data': 'thing 1', 'user__testbridge__bridge_data': 'bridge 1'}
{'user__user_data': 'user', 'thing_data': 'thing 1', 'user__testbridge__bridge_data': 'bridge 2'}
{'user__user_data': 'user', 'thing_data': 'thing 2', 'user__testbridge__bridge_data': 'bridge 1'}
{'user__user_data': 'user', 'thing_data': 'thing 2', 'user__testbridge__bridge_data': 'bridge 2'}
What I expect:
{'user__user_data': 'user', 'thing_data': 'thing 1', 'user__testbridge__bridge_data': 'bridge 1'}
{'user__user_data': 'user', 'thing_data': 'thing 2', 'user__testbridge__bridge_data': 'bridge 2'}
How do I get rid of the relationships between thing 1 and bridge 2, and between thing 2 and bridge 1?
Thanks for your responses.
-Don