Monday, 26 August 2013

how to model a reflexive many to many relationship?

how to model a reflexive many to many relationship?

I'm trying to build an e-learning platform
I have users (Utilisateur) who can take several courses ,each course have
several modules and a module can be in several courses
A user can be a student or teacher or admin, a student is "managed" by a
teacher or several and a teacher can also be managed by a teacher (if he
is a researcher) ,teachers are managed by admins
This is my conception :

I'm not familiar with many_to_many and through concept, please correct me
This is my django models :
class Utilisateur(models.Model):
user = models.OneToOneField(User)
role = models.CharField(choices=ROLES,blank=False,max_length=50)
managed_by = models.ManyToManyField('self',
through='UtilisateurRelationship',
symmetrical=False,
blank=True)
class UtilisateurRelationship(models.Model):
managed = models.ForeignKey(Utilisateur)
manager = models.ForeignKey(Utilisateur)
class Course(models.Model):
titre = models.CharField(blank=False,max_length=100)
class ModuleCourse (models.Model):
module = models.ForeignKey(Module)
course = models.ForeignKey(Course)
order = models.IntegerField(blank=False)
class Module(models.Model):
titre = models.CharField(blank=False,max_length=100)
belong_to = models.ManyToManyField(Course, through='ModuleCourse')
class UtilisateurModule (models.Model):
user = models.ForeignKey(Utilisateur)
module = models.ForeignKey(Module)
score = models.IntegerField(blank=False)
My questions :
How to model the reflexive many to many relationship in users, the one I
called UtilisateurRelationship
How to model the other many to many relationships when I need to add a new
field, example : each module have an order specific to the course it
belongs to, it may be order 2 in course A but order 10 in course B
I need to add a many to many relationship between course and user because
I wouldnt tell which course affected to which user only by affecting a
module to him knowing that the module can belong to several courses, can I
do this?

No comments:

Post a Comment