2👍
Your group leader should NOT be part of the M2M relation, this is just a metadata
. So just add group
into the Membership
class.
class Person(models.Model):
name = models.CharField(max_length=50)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(
Person,
through='Membership',
)
class GroupLeaders(models.Model)
identity = models.ForeignKey(Person)
group = models.ForeignKey(Group)
class Membership(models.Model):
group = models.ForeignKey(Group, on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
assignee = models.ForeignKey(GroupLeaders)
or you can even completely strip out the GroupLeaders
class
class Person(models.Model):
name = models.CharField(max_length=50)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(
Person,
through='Membership',
through_fields=('group', 'person'),
)
class Membership(models.Model):
group = models.ForeignKey(Group, on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
leader = models.ForeignKey(Person, on_delete=models.CASCADE,
related_name="leading_memberships",
)
Source:stackexchange.com