I am working in Django and I'm having some trouble enforcing a particular constraint on my data models. I have a model that consists of a group of members.
class Member:
name = models.CharField(max_length=100)
class Team:
members = models.ManyToManyField(Member)
I want to enforce a constraint such that, for any given team, the set of members is unique. That is, the following teams are allowed:
team_a: {A, B, C}
team_b: {A, B, D}
But a team_c: {C, B, A} is not allowed because the set of members is the same as team_a.
How can I enforce this constraint such that an error is thrown if I try to add team_c (or any other team with the same set of members)?
I have considered having a foreign key for each member and then using unique_together, but this doesn't work because I'd like the model to be unaware of ordering within the members (as there is no ordering inherent in the data). I think this might be a job for the m2m_changed signal, but I haven't been able to get it to work properly.