django - how to query a many to many relationship? -


i'm trying build e-learning platform

i have users (utilisateur) can take several courses ,each course have several modules , module can in several courses

a user can student or teacher or admin, student "managed" teacher or several , teacher can managed teacher (if researcher) ,teachers managed admins

i'm not familiar many_to_many concept, please correct me

this 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',                                           related_name='managers',                                          symmetrical=false,                                           blank=true)     course     = models.manytomanyfield('course')   #can it? (question 2)     module     = models.manytomanyfield('module', through='utilisateurmodule')   class course(models.model):     titre  = models.charfield(blank=false,max_length=100)  class module(models.model):     titre     = models.charfield(blank=false,max_length=100)     course    = models.manytomanyfield(course, through='modulecourse')  class modulecourse (models.model):     module = models.foreignkey(module)     course = models.foreignkey(course)     order  = models.integerfield(blank=false)      class utilisateurmodule (models.model):     user   = models.foreignkey(utilisateur)     module = models.foreignkey(module)     score  = models.integerfield(blank=false) 

my questions :

1. how scores of user module , display them course, :

-course   *module 1      score : 5   *module 2 :    score : 8   ...  -course b   *module 1:     score 7   .... 

2. need add many many relationship between course , user because wouldnt tell course affected user affecting module him knowing module can belong several courses, correct add course = models.manytomanyfield('course') in utilisateur?

ad 2. yes, it's correct. have keep in mind when user starts courses, , of them have same module(s) store 1 mark modules. prevent need create class this:

class utilisateurcoursemodule(models.model):     user   = models.foreignkey(utilisateur)     course = models.foreignkey(course)     module = models.foreignkey(module)     score = models.integerfield() 

ad 1.

for c in user.course.all(): # why course not courses?     print "course {}".format(c.titre)     m in c.module_set.all().order_by('modulecourse__order')         print "module {}".format(m.titre)         try:             print 'score: ', utilisateurmodule.objects.get(user=user,module=m).score         catch utilisateurmodule.doesnotexits:             print 'score: ---' 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -