I'm begginer at django and I knew that you can use ManyToManyField to link two models like author-post user-groups. so I have this relation in my UML
How can I implement this in my code ? my shot was like this
class User(models.Model):
# user fields
class Group(models.Model):
# group fields
class GroupMember(models.Model):
group = models.ForeignKey(Group, on_delete=models.DO_NOTHING, db_index=True)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_index=True)
IsAdmin = models.BooleanField(default=False, verbose_name='Est un admin')
class Meta:
unique_together = (("group", "user"),)
I want to use ManyToManyFields so I can refer between them (if I haven't IsAdmin field, I don't have to create the third class 'GroupMember')
You can find the example from the official doc: https://docs.djangoproject.com/en/3.0/topics/db/models/#extra-fields-on-many-to-many-relationships
The intermediate model is associated with the ManyToManyField using the through argument to point to the model that will act as an intermediary.
The Group class in your model should be like:
class Group(models.Model):
# group fields
# ...
users = models.ManyToManyField(User, through='GroupMember')