how to query a many to many relationship?

房东的猫 提交于 2019-12-30 12:38:12

问题


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

I'm not familiar with many_to_many 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', 
                                         related_name='managers',
                                         symmetrical=False, 
                                         blank=True)
    course     = models.ManyToManyField('Course')   #can I do 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 to get the scores of a user by module and display them by course, like that :

-Course A

 *Module 1 

   score : 5

 *module 2 :

  score : 8

 ...

-Course B

 *Module 1:

   score 7

 ....

2. 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, is it correct to add a course = models.ManyToManyField('Course') in Utilisateur?


回答1:


Ad 2. Yes, it's correct. But you have to keep in mind that when user starts some courses, and some of them have the same module(s) you could store only one mark for those modules. To prevent this you would need to create class like 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)
    for 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: ---'



回答2:


SOLVED

1- This is how I've managed to do it :

    course = Utilisateur.objects.get(user=current_user).course.all() 

    course_sets = []
    for p in course:
        modules = Module.objects.filter(course__id=p.pk).order_by('modulecourse__order') 
        modules_sets = []
        for m in modules:                
            score = UtilisateurModule.objects.get(module=m,utilisateur=current_user)
            modules_sets.append((m, score))
        course_sets.append((p, modules_sets)) 

2- I added course = models.ManyToManyField('Course') to Utilisateur

and then added some manual verification that whenever A course is affected to a user the modules in that course are also affected to the same user in UtilisateurModule



来源:https://stackoverflow.com/questions/18442797/how-to-query-a-many-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!